-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3211.java
67 lines (61 loc) · 2.05 KB
/
_3211.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.fishercoder.solutions.fourththousand;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class _3211 {
public static class Solution1 {
public List<String> validStrings(int n) {
List<String> result = new ArrayList<>();
for (int i = n / 2; i <= n; i++) {
List<String> combinations = generateCombinations(i, n - i);
for (String s : combinations) {
if (noAdjacentZeroes(s)) {
result.add(s);
}
}
}
return result;
}
private boolean noAdjacentZeroes(String s) {
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == '0' && s.charAt(i + 1) == '0') {
return false;
}
}
return true;
}
private List<String> generateCombinations(int ones, int zeroes) {
int[] nums = new int[ones + zeroes];
int i = 0;
while (ones-- > 0) {
nums[i++] = 1;
}
return permuteUnique(nums);
}
private List<String> permuteUnique(int[] nums) {
Set<String> set = new HashSet<>();
set.add("");
set = recurse(nums, set, 0);
List<String> list = new ArrayList<>();
for (String s : set) {
list.add(s);
}
return list;
}
private Set<String> recurse(int[] nums, Set<String> set, int pos) {
if (pos == nums.length) {
return set;
}
Set<String> newSet = new HashSet<>();
for (String s : set) {
for (int i = 0; i <= s.length(); i++) {
StringBuilder sb = new StringBuilder(s);
sb.insert(i, nums[pos]);
newSet.add(sb.toString());
}
}
return recurse(nums, newSet, pos + 1);
}
}
}