File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Expand file tree Collapse file tree 1 file changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+ private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+ private static Map<Character, Integer> map;
9+ private static char[] input;
10+ private static int N;
11+ public static void main(String[] args) throws IOException {
12+ init();
13+
14+ int answer = twoPointer();
15+
16+ bw.write(answer + "\n");
17+ bw.flush();
18+ bw.close();
19+ br.close();
20+ }
21+
22+ private static void init() throws IOException {
23+ N = Integer.parseInt(br.readLine());
24+ map = new HashMap<>();
25+ input = br.readLine().toCharArray();
26+ }
27+
28+ private static int twoPointer() {
29+ int left = 0;
30+ int right = 0;
31+ int max = 0;
32+ int count = 0;
33+
34+ while (left < input.length) {
35+ if (right < input.length && valid(input[right])) {
36+ map.put(input[right], map.getOrDefault(input[right], 0)+1);
37+ count++;
38+ max = Math.max(max, count);
39+ right++;
40+ } else {
41+ map.put(input[left], map.get(input[left])-1);
42+ if (map.get(input[left]) == 0) {
43+ map.remove(input[left]);
44+ }
45+ left++;
46+ count--;
47+ }
48+ }
49+
50+ return max;
51+ }
52+
53+ private static boolean valid(char c) {
54+ return (map.containsKey(c) && map.size() <= N) || (!map.containsKey(c) && map.size() < N);
55+ }
56+
57+ }
58+ ```
You can’t perform that action at this time.
0 commit comments