Skip to content

Commit 6a78f05

Browse files
authored
Create 1898-maximum-number-of-removable-characters.kt
1 parent 38b9c58 commit 6a78f05

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
fun maximumRemovals(s: String, p: String, removable: IntArray): Int {
3+
4+
fun isSubseq(removed: HashSet<Int>): Boolean {
5+
var i = 0
6+
var j = 0
7+
8+
while (i < s.length && j < p.length) {
9+
if (i in removed || s[i] != p[j]) {
10+
i++
11+
continue
12+
}
13+
i++
14+
j++
15+
}
16+
17+
return j == p.length
18+
}
19+
20+
var res = 0
21+
var l = 0
22+
var r = removable.lastIndex
23+
while (l <= r) {
24+
val m = (l + r) / 2
25+
26+
val removed = HashSet<Int>()
27+
for (i in 0..m)
28+
removed.add(removable[i])
29+
30+
if (isSubseq(removed)) {
31+
res = maxOf(res, m + 1)
32+
l = m + 1
33+
} else {
34+
r = m - 1
35+
}
36+
}
37+
38+
return res
39+
}
40+
}

0 commit comments

Comments
 (0)