Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit545caed

Browse files
Merge pull requestneetcode-gh#3346 from adhamahmad/Create-1898-Maximum-Number-of-Removable-Characters.java
Create: 1898-maximum-number-of-removable-characters.java
2 parents4d03438 +14a1eae commit545caed

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
classSolution {
2+
3+
publicintmaximumRemovals(Strings,Stringp,int[]removable) {
4+
intleft =0;
5+
intright =removable.length -1;
6+
intresult =0;
7+
while(left <=right){
8+
HashSet<Integer>removed =newHashSet<>();
9+
intmid = (left+right)/2;
10+
for(inti =0;i <=mid;i++){// populate the hashset
11+
removed.add(removable[i]);
12+
}
13+
if(isSubseq(s,p,removed)){
14+
result =Math.max(result,mid+1);
15+
left =mid+1;// greedy try to find even a higher value
16+
}else{
17+
right =mid -1;
18+
}
19+
}
20+
returnresult;
21+
}
22+
23+
privatebooleanisSubseq(Strings ,Stringsubseq,HashSet<Integer>removed){
24+
inti1 =0;
25+
inti2 =0;
26+
while(i1 <s.length() &&i2 <subseq.length()){
27+
if(s.charAt(i1) !=subseq.charAt(i2) ||removed.contains(i1) ){
28+
i1++;
29+
}else{
30+
i1++;
31+
i2++;
32+
}
33+
}
34+
returni2 ==subseq.length();
35+
}
36+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp