Last active
December 10, 2015 19:18
-
-
Save gpaulu/4480241 to your computer and use it in GitHub Desktop.
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
import java.util.LinkedHashSet; | |
public class Permutation { | |
private LinkedHashSet<String> set; | |
private String s; | |
private boolean found; | |
Permutation(String s){ | |
this.s = s; | |
found = false; | |
set = new LinkedHashSet<String>(); | |
} | |
Permutation(String s, boolean run){ | |
this(s); | |
if(run) | |
run(); | |
} | |
public LinkedHashSet<String> getPurmutations(){ | |
if(found) | |
return set; | |
else{ | |
run(); | |
return set; | |
} | |
} | |
public void run(){ | |
recPerms("",s); | |
found = true; | |
} | |
private void recPerms(String begin, String end){ | |
if(end.isEmpty()){ | |
set.add(begin); | |
} | |
else{ | |
for(int i=0; i<end.length();i++){ | |
recPerms(begin + String.valueOf(end.charAt(i)),end.substring(0, i)+end.substring(i+1)); | |
} | |
} | |
} | |
@Override | |
public String toString(){ | |
if(found) | |
return set.toString(); | |
else{ | |
run(); | |
return set.toString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment