Skip to content

Instantly share code, notes, and snippets.

@gabhi
Last active August 29, 2015 13:59
Show Gist options
  • Save gabhi/10443043 to your computer and use it in GitHub Desktop.
Save gabhi/10443043 to your computer and use it in GitHub Desktop.
String Permutations
permuteString("", "ABCD");
//if endString.lenth <=1 then print
//for each character in endString
//Remove ith character
//permute i, remaining string
public static void permuteString(String beginningString, String endingString) {
if (endingString.length() <= 1)
System.out.println(beginningString + endingString);
else
for (int i = 0; i < endingString.length(); i++) {
try {
String newString = endingString.substring(0, i) + endingString.substring(i + 1);
permuteString(beginningString + endingString.charAt(i), newString);
} catch (StringIndexOutOfBoundsException exception) {
exception.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment