Skip to content

Instantly share code, notes, and snippets.

@gourab5139014
Created February 2, 2014 12:26
Show Gist options
  • Save gourab5139014/8767638 to your computer and use it in GitHub Desktop.
Save gourab5139014/8767638 to your computer and use it in GitHub Desktop.
Permutations of a String by Recursion using Java
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); //current character removed from the new string
permuteString(beginningString + endingString.charAt(i), newString);
} catch (Exception e) { System.err.println(e.getMessage()); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment