Last active
August 29, 2015 13:59
-
-
Save gabhi/10443043 to your computer and use it in GitHub Desktop.
String Permutations
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
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