-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNyanCat.java
More file actions
41 lines (31 loc) · 1.2 KB
/
NyanCat.java
File metadata and controls
41 lines (31 loc) · 1.2 KB
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
package stonehee.baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class NyanCat {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String str = br.readLine();
System.out.println(findMaxLength(str, N));
}
private static int findMaxLength(String str, int N) {
Map<Character, Integer> charCount = new HashMap<>();
int start = 0, maxLength = 0;
for (int end = 0; end < str.length(); end++) {
charCount.put(str.charAt(end), charCount.getOrDefault(str.charAt(end), 0) + 1);
while (charCount.size() > N) {
char startChar = str.charAt(start);
charCount.put(startChar, charCount.get(startChar) - 1);
if (charCount.get(startChar) == 0) {
charCount.remove(startChar);
}
start++;
}
maxLength = Math.max(maxLength, end - start + 1);
}
return maxLength;
}
}