Skip to content

Commit e0c14fc

Browse files
add 3211
1 parent c32daf2 commit e0c14fc

File tree

2 files changed

+68
-0
lines changed
  • paginated_contents/algorithms/4th_thousand
  • src/main/java/com/fishercoder/solutions/fourththousand

2 files changed

+68
-0
lines changed

paginated_contents/algorithms/4th_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| # | Title | Solutions | Video | Difficulty | Tag
22
|------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------|------------|----------------------------------------------------------------------
3+
| 3211 | [Generate Binary Strings Without Adjacent Zeros](https://leetcode.com/problems/generate-binary-strings-without-adjacent-zeros/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3211.java) | | Medium | Recursion, Backtracking
34
| 3210 | [Find the Encrypted String](https://leetcode.com/problems/find-the-encrypted-string/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3210.java) | | Easy |
45
| 3208 | [Alternating Groups II](https://leetcode.com/problems/alternating-groups-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3208.java) | | Medium |
56
| 3206 | [Alternating Groups I](https://leetcode.com/problems/alternating-groups-i/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3206.java) | | Easy |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
public class _3211 {
9+
public static class Solution1 {
10+
public List<String> validStrings(int n) {
11+
List<String> result = new ArrayList<>();
12+
for (int i = n / 2; i <= n; i++) {
13+
List<String> combinations = generateCombinations(i, n - i);
14+
for (String s : combinations) {
15+
if (noAdjacentZeroes(s)) {
16+
result.add(s);
17+
}
18+
}
19+
}
20+
return result;
21+
}
22+
23+
private boolean noAdjacentZeroes(String s) {
24+
for (int i = 0; i < s.length() - 1; i++) {
25+
if (s.charAt(i) == '0' && s.charAt(i + 1) == '0') {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
32+
private List<String> generateCombinations(int ones, int zeroes) {
33+
int[] nums = new int[ones + zeroes];
34+
int i = 0;
35+
while (ones-- > 0) {
36+
nums[i++] = 1;
37+
}
38+
return permuteUnique(nums);
39+
}
40+
41+
private List<String> permuteUnique(int[] nums) {
42+
Set<String> set = new HashSet<>();
43+
set.add("");
44+
set = recurse(nums, set, 0);
45+
List<String> list = new ArrayList<>();
46+
for (String s : set) {
47+
list.add(s);
48+
}
49+
return list;
50+
}
51+
52+
private Set<String> recurse(int[] nums, Set<String> set, int pos) {
53+
if (pos == nums.length) {
54+
return set;
55+
}
56+
Set<String> newSet = new HashSet<>();
57+
for (String s : set) {
58+
for (int i = 0; i <= s.length(); i++) {
59+
StringBuilder sb = new StringBuilder(s);
60+
sb.insert(i, nums[pos]);
61+
newSet.add(sb.toString());
62+
}
63+
}
64+
return recurse(nums, newSet, pos + 1);
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)