File tree 4 files changed +86
-6
lines changed
solution/1400-1499/1461.Check If a String Contains All Binary Codes of Size K
4 files changed +86
-6
lines changed Original file line number Diff line number Diff line change 44
44
<strong >输出:</strong >false
45
45
</pre >
46
46
47
- <p >  ; </p >
48
-
49
47
<p ><strong >提示:</strong ></p >
50
48
51
49
<ul >
58
56
59
57
<!-- 这里可写通用的实现逻辑 -->
60
58
59
+ 遍历字符串 s,用一个 set 存储所有长度为 k 的不同子串。只需要判断子串数能否达到 2<sup >k</sup > 即可。
60
+
61
61
<!-- tabs:start -->
62
62
63
63
### ** Python3**
64
64
65
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
66
66
67
67
``` 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
69
79
```
70
80
71
81
### ** Java**
72
82
73
83
<!-- 这里可写当前语言的特殊实现逻辑 -->
74
84
75
85
``` 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
+ }
77
103
```
78
104
79
105
### ** ...**
Original file line number Diff line number Diff line change 63
63
### ** Python3**
64
64
65
65
``` 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
67
77
```
68
78
69
79
### ** Java**
70
80
71
81
``` 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
+ }
73
99
```
74
100
75
101
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments