File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments