-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxNumberOfRemovable.java
55 lines (51 loc) · 1.17 KB
/
MaxNumberOfRemovable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class MaxNumberOfRemovable
{
public static void main(String[] args)
{
System.out.println(maximumRemovals("abcacb", "ab", new int[]{3,1,0}));
}
static int maximumRemovals(String s, String p, int[] removable)
{
int start = 0;
int end = removable.length-1;
while (start<=end)
{
int mid = start+(end-start)/2;
StringBuffer buffer = new StringBuffer(s);
for (int i = 0; i <= mid; i++)
{
buffer.setCharAt(removable[i], ' ');
}
if (!isSubsequence(buffer.toString(), p)) end = mid-1;
else start = mid+1;
}
return end+1;
}
static boolean isSubsequence(String mainString, String pattern)
{
int pointerForMainString = 0;
int pointerForPattern = 0;
while (pointerForMainString<pattern.length() && pointerForPattern<mainString.length())
{
if(mainString.charAt(pointerForPattern) == pattern.charAt(pointerForMainString))
{
pointerForMainString++;
}
pointerForPattern++;
}
return pointerForMainString == pattern.length();
}
}
/*
* int n = p.length();
int m = s.length();
int i = 0;
int j = 0;
while (i < n && j < m) {
if (p.charAt(i) == s.charAt(j)) {
i++;
}
j++;
}
return i == n;
*/