Skip to content

Commit cf574fc

Browse files
committed
feat: add python and java solutions to leetcode problem: No.1461
1461. Check If a String Contains All Binary Codes of Size K
1 parent f7eb6f8 commit cf574fc

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

solution/1400-1499/1461.Check If a String Contains All Binary Codes of Size K/README.md

+30-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
<strong>输出:</strong>false
4545
</pre>
4646

47-
<p>&nbsp;</p>
48-
4947
<p><strong>提示:</strong></p>
5048

5149
<ul>
@@ -58,22 +56,50 @@
5856

5957
<!-- 这里可写通用的实现逻辑 -->
6058

59+
遍历字符串 s,用一个 set 存储所有长度为 k 的不同子串。只需要判断子串数能否达到 2<sup>k</sup> 即可。
60+
6161
<!-- tabs:start -->
6262

6363
### **Python3**
6464

6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```python
68-
68+
class Solution:
69+
def hasAllCodes(self, s: str, k: int) -> bool:
70+
counter = 1 << k
71+
exists = set()
72+
for i in range(k, len(s) + 1):
73+
if s[i - k: i] not in exists:
74+
exists.add(s[i - k: i])
75+
counter -= 1
76+
if counter == 0:
77+
return True
78+
return False
6979
```
7080

7181
### **Java**
7282

7383
<!-- 这里可写当前语言的特殊实现逻辑 -->
7484

7585
```java
76-
86+
class Solution {
87+
public boolean hasAllCodes(String s, int k) {
88+
int counter = 1 << k;
89+
Set<String> exists = new HashSet<>();
90+
for (int i = k; i <= s.length(); ++i) {
91+
String t = s.substring(i - k, i);
92+
if (!exists.contains(t)) {
93+
exists.add(t);
94+
--counter;
95+
}
96+
if (counter == 0) {
97+
return true;
98+
}
99+
}
100+
return false;
101+
}
102+
}
77103
```
78104

79105
### **...**

solution/1400-1499/1461.Check If a String Contains All Binary Codes of Size K/README_EN.md

+28-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,39 @@
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def hasAllCodes(self, s: str, k: int) -> bool:
68+
counter = 1 << k
69+
exists = set()
70+
for i in range(k, len(s) + 1):
71+
if s[i - k: i] not in exists:
72+
exists.add(s[i - k: i])
73+
counter -= 1
74+
if counter == 0:
75+
return True
76+
return False
6777
```
6878

6979
### **Java**
7080

7181
```java
72-
82+
class Solution {
83+
public boolean hasAllCodes(String s, int k) {
84+
int counter = 1 << k;
85+
Set<String> exists = new HashSet<>();
86+
for (int i = k; i <= s.length(); ++i) {
87+
String t = s.substring(i - k, i);
88+
if (!exists.contains(t)) {
89+
exists.add(t);
90+
--counter;
91+
}
92+
if (counter == 0) {
93+
return true;
94+
}
95+
}
96+
return false;
97+
}
98+
}
7399
```
74100

75101
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public boolean hasAllCodes(String s, int k) {
3+
int counter = 1 << k;
4+
Set<String> exists = new HashSet<>();
5+
for (int i = k; i <= s.length(); ++i) {
6+
String t = s.substring(i - k, i);
7+
if (!exists.contains(t)) {
8+
exists.add(t);
9+
--counter;
10+
}
11+
if (counter == 0) {
12+
return true;
13+
}
14+
}
15+
return false;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def hasAllCodes(self, s: str, k: int) -> bool:
3+
counter = 1 << k
4+
exists = set()
5+
for i in range(k, len(s) + 1):
6+
if s[i - k: i] not in exists:
7+
exists.add(s[i - k: i])
8+
counter -= 1
9+
if counter == 0:
10+
return True
11+
return False

0 commit comments

Comments
 (0)