Skip to content

Commit 5e706e0

Browse files
committed
Resolve "Check If a String Contains All Binary Codes of Size K" problem
1 parent a450e85 commit 5e706e0

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/main/java/com/github/dkoval/leetcode/challenge/CheckIfStringContainsAllBinaryCodesOfSizeK.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.Set;
55

66
/**
7-
* <a href="https://leetcode.com/explore/challenge/card/march-leetcoding-challenge-2021/589/week-2-march-8th-march-14th/3669/">Check If a String Contains All Binary Codes of Size K</a>
7+
* <a href="https://leetcode.com/problems/check-if-a-string-contains-all-binary-codes-of-size-k/">Check If a String Contains All Binary Codes of Size K</a>
88
* <p>
99
* Given a binary string s and an integer k.
1010
* <p>
@@ -55,7 +55,7 @@ private String binCode(int x, int length) {
5555
}
5656
}
5757

58-
class CheckIfStringContainsAllBinaryCodesOfSizeKAccepted implements CheckIfStringContainsAllBinaryCodesOfSizeK {
58+
class CheckIfStringContainsAllBinaryCodesOfSizeKAccepted implements CheckIfStringContainsAllBinaryCodesOfSizeK {
5959

6060
public boolean hasAllCodes(String s, int k) {
6161
// Sliding window: for each i in [0, N - k], where N is the length of string s,
@@ -76,6 +76,23 @@ public boolean hasAllCodes(String s, int k) {
7676
}
7777
}
7878

79+
class CheckIfStringContainsAllBinaryCodesOfSizeKRev2 implements CheckIfStringContainsAllBinaryCodesOfSizeK {
80+
81+
@Override
82+
public boolean hasAllCodes(String s, int k) {
83+
final var n = s.length();
84+
85+
// generate all possible substrings of length k
86+
final var substrs = new HashSet<>();
87+
for (var start = 0; start <= n - k; start++) {
88+
substrs.add(s.substring(start, start + k));
89+
}
90+
91+
// the total number of such substrings should be 2^k
92+
return substrs.size() == (1 << k);
93+
}
94+
}
95+
7996
class CheckIfStringContainsAllBinaryCodesOptimized implements CheckIfStringContainsAllBinaryCodesOfSizeK {
8097

8198
@Override

src/test/kotlin/com/github/dkoval/leetcode/challenge/CheckIfStringContainsAllBinaryCodesOfSizeKTest.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ internal class CheckIfStringContainsAllBinaryCodesOfSizeKTest {
7676
}
7777
}
7878

79+
@Nested
80+
inner class CheckIfStringContainsAllBinaryCodesOfSizeKRev2Test {
81+
82+
@ParameterizedTest
83+
@ArgumentsSource(InputArgumentsProvider::class)
84+
fun `should return True if every binary code of length k is a substring of s`(
85+
s: String,
86+
k: Int,
87+
expected: Boolean
88+
) {
89+
CheckIfStringContainsAllBinaryCodesOfSizeKRev2().test(s, k, expected)
90+
}
91+
}
92+
7993
@Nested
8094
inner class CheckIfStringContainsAllBinaryCodesOptimizedTest {
8195

@@ -90,9 +104,9 @@ internal class CheckIfStringContainsAllBinaryCodesOfSizeKTest {
90104
CheckIfStringContainsAllBinaryCodesOptimized().test(s, k, expected)
91105
}
92106
}
107+
}
93108

94-
private fun CheckIfStringContainsAllBinaryCodesOfSizeK.test(s: String, k: Int, expected: Boolean) {
95-
val actual = hasAllCodes(s, k)
96-
assertEquals(expected, actual)
97-
}
98-
}
109+
private fun CheckIfStringContainsAllBinaryCodesOfSizeK.test(s: String, k: Int, expected: Boolean) {
110+
val actual = hasAllCodes(s, k)
111+
assertEquals(expected, actual)
112+
}

0 commit comments

Comments
 (0)