@@ -62,46 +62,44 @@ Hence, the maximum k is 2.
62
62
``` python
63
63
class Solution :
64
64
def maximumRemovals (self , s : str , p : str , removable : List[int ]) -> int :
65
- def isSubsequence (mid ):
66
- m, n = len (s), len (p)
67
- i = j = 0
68
- ids = {e for e in removable[:mid]}
65
+ def check (mid ):
66
+ m, n, i, j = len (s), len (p), 0 , 0
67
+ ids = set (removable[:mid])
69
68
while i < m and j < n:
70
69
if i not in ids and s[i] == p[j]:
71
70
j += 1
72
71
i += 1
73
72
return j == n
74
73
75
- low, high = 0 , len (removable)
76
- while low < high :
77
- mid = (low + high + 1 ) >> 1
78
- if isSubsequence (mid):
79
- low = mid
74
+ left, right = 0 , len (removable)
75
+ while left < right :
76
+ mid = (left + right + 1 ) >> 1
77
+ if check (mid):
78
+ left = mid
80
79
else :
81
- high = mid - 1
82
- return low
80
+ right = mid - 1
81
+ return left
83
82
```
84
83
85
84
### ** Java**
86
85
87
86
``` java
88
87
class Solution {
89
88
public int maximumRemovals (String s , String p , int [] removable ) {
90
- int low = 0 , high = removable. length;
91
- while (low < high ) {
92
- int mid = (low + high + 1 ) >> 1 ;
93
- if (isSubsequence (s, p, removable, mid)) {
94
- low = mid;
89
+ int left = 0 , right = removable. length;
90
+ while (left < right ) {
91
+ int mid = (left + right + 1 ) >> 1 ;
92
+ if (check (s, p, removable, mid)) {
93
+ left = mid;
95
94
} else {
96
- high = mid - 1 ;
95
+ right = mid - 1 ;
97
96
}
98
97
}
99
- return low ;
98
+ return left ;
100
99
}
101
100
102
- private boolean isSubsequence (String s , String p , int [] removable , int mid ) {
103
- int m = s. length(), n = p. length();
104
- int i = 0 , j = 0 ;
101
+ private boolean check (String s , String p , int [] removable , int mid ) {
102
+ int m = s. length(), n = p. length(), i = 0 , j = 0 ;
105
103
Set<Integer > ids = new HashSet<> ();
106
104
for (int k = 0 ; k < mid; ++ k) {
107
105
ids. add(removable[k]);
@@ -146,6 +144,73 @@ function isSub(str: string, sub: string, idxes: Set<number>): boolean {
146
144
}
147
145
```
148
146
147
+ ### ** C++**
148
+
149
+ ``` cpp
150
+ class Solution {
151
+ public:
152
+ int maximumRemovals(string s, string p, vector<int >& removable) {
153
+ int left = 0, right = removable.size();
154
+ while (left < right) {
155
+ int mid = left + right + 1 >> 1;
156
+ if (check(s, p, removable, mid)) {
157
+ left = mid;
158
+ } else {
159
+ right = mid - 1;
160
+ }
161
+ }
162
+ return left;
163
+ }
164
+
165
+ bool check(string s, string p, vector<int>& removable, int mid) {
166
+ int m = s.size(), n = p.size(), i = 0, j = 0;
167
+ unordered_set<int> ids;
168
+ for (int k = 0; k < mid; ++k) {
169
+ ids.insert(removable[k]);
170
+ }
171
+ while (i < m && j < n) {
172
+ if (ids.count(i) == 0 && s[i] == p[j]) {
173
+ ++j;
174
+ }
175
+ ++i;
176
+ }
177
+ return j == n;
178
+ }
179
+ };
180
+ ```
181
+
182
+ ### ** Go**
183
+
184
+ ``` go
185
+ func maximumRemovals (s string , p string , removable []int ) int {
186
+ left , right := 0 , len (removable)
187
+ for left < right {
188
+ mid := (left + right + 1 ) >> 1
189
+ if check (s, p, removable, mid) {
190
+ left = mid
191
+ } else {
192
+ right = mid - 1
193
+ }
194
+ }
195
+ return left
196
+ }
197
+
198
+ func check (s string , p string , removable []int , mid int ) bool {
199
+ m , n , i , j := len (s), len (p), 0 , 0
200
+ ids := make (map [int ]bool )
201
+ for k := 0 ; k < mid; k++ {
202
+ ids[removable[k]] = true
203
+ }
204
+ for i < m && j < n {
205
+ if !ids[i] && s[i] == p[j] {
206
+ j++
207
+ }
208
+ i++
209
+ }
210
+ return j == n
211
+ }
212
+ ```
213
+
149
214
### ** ...**
150
215
151
216
```
0 commit comments