Skip to content

Commit 650123e

Browse files
committed
2022-05-31 update: added "Check If a String Contains All Binary Codes of Size K"
1 parent bdf370d commit 650123e

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
// https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/
7+
public class CheckIfAStringContainsAllBinaryCodesOfSizeK {
8+
9+
private final String s;
10+
private final int k;
11+
12+
public CheckIfAStringContainsAllBinaryCodesOfSizeK(String s, int k) {
13+
this.s = s;
14+
this.k = k;
15+
}
16+
17+
public boolean solution() {
18+
int need = 1 << k;
19+
Set<String> exists = new HashSet<>();
20+
for (int i = k; i <= s.length(); i++) {
21+
String str = s.substring(i - k, i);
22+
if (!exists.contains(str)) {
23+
exists.add(str);
24+
need--;
25+
if (need == 0) {
26+
return true;
27+
}
28+
}
29+
}
30+
return false;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class CheckIfAStringContainsAllBinaryCodesOfSizeKTest {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertTrue(
12+
new CheckIfAStringContainsAllBinaryCodesOfSizeK(
13+
"00110110",
14+
2
15+
).solution()
16+
);
17+
}
18+
19+
}

0 commit comments

Comments
 (0)