From 7aa645eb800b11ec9c031d39acb688c2c2d36543 Mon Sep 17 00:00:00 2001 From: Robyn Cute Date: Tue, 11 Oct 2016 18:20:01 -0400 Subject: [PATCH 1/2] Print all permutations of a string --- src/TechInterview/StringPermutation | 35 +++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/TechInterview/StringPermutation diff --git a/src/TechInterview/StringPermutation b/src/TechInterview/StringPermutation new file mode 100644 index 0000000..ff3a9f9 --- /dev/null +++ b/src/TechInterview/StringPermutation @@ -0,0 +1,35 @@ +/* + * Cracking the Code Interview + * + * Question: Generate all Permutations for a string. + * Uses recursion to calculate this. + */ + +import java.util.Scanner; + + +public class permutation { + + public static void permutation(String str) { + permutation("", str); + } + + private static void permutation(String prefix, String str) { + int n = str.length(); + if (n == 0) System.out.println(prefix); + else { + for (int i = 0; i < n; i++) + permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n)); + } + } + + public static void main(String[] args) { + System.out.println("Please enter a string: "); + Scanner sc = new Scanner(System.in); + String permString = sc.nextLine(); + System.out.println("All possible permuations for "+ permString +":"); + permutation(permString); + + } + +} From 0580ba47a9e6170871b486e0f7a26257ea982eb5 Mon Sep 17 00:00:00 2001 From: Robyn Cute Date: Tue, 11 Oct 2016 18:20:32 -0400 Subject: [PATCH 2/2] Rename StringPermutation to StringPermutation.java --- src/TechInterview/{StringPermutation => StringPermutation.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/TechInterview/{StringPermutation => StringPermutation.java} (100%) diff --git a/src/TechInterview/StringPermutation b/src/TechInterview/StringPermutation.java similarity index 100% rename from src/TechInterview/StringPermutation rename to src/TechInterview/StringPermutation.java