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

Commit8738cee

Browse files
authored
Create Take K of Each Character From Left and Right.java
1 parent5eaf013 commit8738cee

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
classSolution {
2+
publicinttakeCharacters(Strings,intk) {
3+
int[]counter =newint[3];
4+
intn =s.length();
5+
for (charc :s.toCharArray()) {
6+
counter[c -'a']++;
7+
}
8+
for (inti =0;i <3;i++) {
9+
if (counter[i] <k) {
10+
return -1;
11+
}
12+
}
13+
int[]window =newint[3];
14+
intleft =0;
15+
intmaxWindow =0;
16+
for (intright =0;right <n;right++) {
17+
window[s.charAt(right) -'a']++;
18+
while (left <=right &&
19+
(counter[0] -window[0] <k ||counter[1] -window[1] <k ||counter[2] -window[2] <k)) {
20+
window[s.charAt(left) -'a']--;
21+
left++;
22+
}
23+
maxWindow =Math.max(maxWindow,right -left +1);
24+
}
25+
returnn -maxWindow;
26+
}
27+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp