Skip to content

Instantly share code, notes, and snippets.

@jmhertlein
Created July 25, 2015 18:11
Show Gist options
  • Save jmhertlein/7f4be95229e65a9c3d40 to your computer and use it in GitHub Desktop.
Save jmhertlein/7f4be95229e65a9c3d40 to your computer and use it in GitHub Desktop.
import java.util.List;
import java.util.LinkedList;
public class Permute {
public static void main(String[] args) {
for(String s : permute("abcd"))
System.out.println(s);
}
public static List<String> permute(String s) {
List<String> ret = new LinkedList<String>();
if(s.isEmpty()) {
ret.add("");
return ret;
}
for(char c : s.toCharArray()) {
for(String subPermute : permute(s.replaceFirst("[" + c + "]", "")))
ret.add(c + subPermute);
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment