(String str)
| 5 | public class Question { |
| 6 | |
| 7 | public static ArrayList<String> getPerms(String str) { |
| 8 | if (str == null) { |
| 9 | return null; |
| 10 | } |
| 11 | ArrayList<String> permutations = new ArrayList<String>(); |
| 12 | if (str.length() == 0) { // base case |
| 13 | permutations.add(""); |
| 14 | return permutations; |
| 15 | } |
| 16 | |
| 17 | char first = str.charAt(0); // get the first character |
| 18 | String remainder = str.substring(1); // remove the first character |
| 19 | ArrayList<String> words = getPerms(remainder); |
| 20 | for (String word : words) { |
| 21 | for (int j = 0; j <= word.length(); j++) { |
| 22 | String s = insertCharAt(word, first, j); |
| 23 | permutations.add(s); |
| 24 | } |
| 25 | } |
| 26 | return permutations; |
| 27 | } |
| 28 | |
| 29 | public static String insertCharAt(String word, char c, int i) { |
| 30 | String start = word.substring(0, i); |
no test coverage detected