MCPcopy Create free account
hub / github.com/careercup/ctci / getPerms

Method getPerms

java/Chapter 9/Question9_5/Question.java:7–27  ·  view source on GitHub ↗
(String str)

Source from the content-addressed store, hash-verified

5public 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);

Callers 1

mainMethod · 0.95

Calls 3

insertCharAtMethod · 0.95
lengthMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected