(int remaining)
| 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); |
no test coverage detected