Created
June 26, 2014 23:29
-
-
Save gabhi/e3ed4220c13f4b071abc to your computer and use it in GitHub Desktop.
String permutations Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringPermutations { | |
public static void main(String[] args) { | |
permuteString("abc"); | |
} | |
private static void permuteString(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)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment