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

Method generateParens

java/Chapter 9/Question9_6/QuestionA.java:13–33  ·  view source on GitHub ↗
(int remaining)

Source from the content-addressed store, hash-verified

11 }
12
13 public static Set<String> generateParens(int remaining) {
14 Set<String> set = new HashSet<String>();
15 if (remaining == 0) {
16 set.add("");
17 } else {
18 Set<String> prev = generateParens(remaining - 1);
19 for (String str : prev) {
20 for (int i = 0; i < str.length(); i++) {
21 if (str.charAt(i) == '(') {
22 String s = insertInside(str, i);
23 /* Add s to set if it is not already in there. Note:
24 * HashSet automatically checks for duplicates before
25 * adding, so an explicit check is not necessary. */
26 set.add(s);
27 }
28 }
29 set.add("()" + str);
30 }
31 }
32 return set;
33 }
34
35 public static void main(String[] args) {
36 Set<String> list = generateParens(4);

Callers 1

mainMethod · 0.95

Calls 3

insertInsideMethod · 0.95
addMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected