Maximum Number of Removable Characters LeetCode solution in Java

1
class Solution {
2
public int maximumRemovals(String s, String p, int[] removable) {
3
int left = 0, right = removable.length;
4

5
while (left < right) {
6
int middle = (right + left + 1) / 2;
7
String afterRemoval = remove(s, removable, middle);
8
if (isSubsequence(p, afterRemoval, p.length(), afterRemoval.length())) left = middle;
9
else right = middle - 1;
10
}
11

12
return left;
13
}
14

15
private String remove(String s, int[] removable, int k) {
16
char[] symbols = s.toCharArray();
17
for (int i = 0; i < k; i++) {
18
symbols[removable[i]] = Character.MIN_VALUE;
19
}
20
return String.valueOf(symbols);
21
}
22

23
private boolean isSubsequence(
24
String subSequence, String word, int subSequenceChar, int wordChar) {
25
if (subSequenceChar == 0) return true;
26
if (wordChar == 0) return false;
27

28
if (subSequence.charAt(subSequenceChar - 1) == word.charAt(wordChar - 1))
29
return isSubsequence(subSequence, word, subSequenceChar - 1, wordChar - 1);
30

31
return isSubsequence(subSequence, word, subSequenceChar, wordChar - 1);
32
}
33
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0