ALGORITHM/BOJ
[BOJ] 16427 고냥이
0298
2023. 6. 26. 20:35
https://www.acmicpc.net/problem/16472
16472번: 고냥이
고양이는 너무 귀엽다. 사람들은 고양이를 너무 귀여워했고, 결국 고양이와 더욱 가까워지고 싶어 고양이와의 소통을 위한 고양이 말 번역기를 발명하기로 했다. 이 번역기는 사람의 언어를 고
www.acmicpc.net
2023-06-26
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
|
import java.util.Scanner;
public class Main16472 {
public static int n;
public static int[] arr, str;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
String cat = sc.next();
arr = new int[26];
str = new int[cat.length()];
for(int i = 0; i < cat.length(); i++) str[i] = cat.charAt(i) - 'a';
int start = 0;
int end = 0;
int answer = 0;
arr[str[0]]++;
int count = 1;
while(start <= end) {
if(count > n) {
arr[str[start]]--;
if(arr[str[start]] == 0) count--;
start++;
} else {
answer = Math.max(answer, end-start+1);
end++;
if(end == str.length) break;
if(arr[str[end]] == 0) count++;
arr[str[end]]++;
}
}
System.out.println(answer);
}
}
|
cs |
#문제풀이
투 포인터
1. 알파벳 종류는 count로 체크
2. count > n 크면 start++
3. count <= n 면 길이 max 체크하고 end++
그리고 end == 총 문자열 길이가 되면 종료