Skip to content

Commit b3b6e86

Browse files
authored
Merge pull request #774 from AlgorithmWithGod/lkhyun
[20250830] BOJ / G4 / 고냥이 / 이강현
2 parents 9eac395 + f482e3b commit b3b6e86

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static int N;
10+
static Map<Character,Integer> cur;
11+
12+
public static void main(String[] args) throws IOException {
13+
N = Integer.parseInt(br.readLine());
14+
String str = br.readLine();
15+
16+
cur = new HashMap<>();
17+
18+
int max = 0;
19+
int ans = 0;
20+
int left = 0, right = 0;
21+
22+
while(right < str.length()){
23+
if(cur.size() < N || (cur.size() == N && cur.containsKey(str.charAt(right)))){
24+
cur.put(str.charAt(right), cur.getOrDefault(str.charAt(right), 0)+1);
25+
ans++;
26+
right++;
27+
max = Math.max(ans,max);
28+
}else{
29+
ans--;
30+
int temp = cur.get(str.charAt(left));
31+
if(temp == 1){
32+
cur.remove(str.charAt(left++));
33+
}else{
34+
cur.put(str.charAt(left++), temp-1);
35+
}
36+
}
37+
}
38+
bw.write(max+"");
39+
bw.close();
40+
}
41+
}
42+
```

0 commit comments

Comments
 (0)