-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31433.java
More file actions
38 lines (35 loc) · 1.12 KB
/
Copy path31433.java
File metadata and controls
38 lines (35 loc) · 1.12 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
// https://www.acmicpc.net/problem/31433
// BOJ 31433 KSA 문자열
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
public static void main(String[] args) throws IOException {
System.out.println(solution());
}
public static int solution(){
Scanner scan = new Scanner(System.in);
return solution(scan.next());
}
public static int solution(String input){
int len = input.length();
int count1 = countPattern(input, "KSA");
int count2 = countPattern(input, "SAK");
if(count2 == len) count2--;
int count3 = countPattern(input, "AKS");
if(count3 >= len-1) count3 = len-2;
int max = Math.max(count1, Math.max(count2, count3));
int answer = 2*(len-max);
return answer;
}
public static int countPattern(String str, String pattern){
int index = 0, count = 0;
for(int i=0; i<str.length(); i++){
if(str.charAt(i) == pattern.charAt(index)){
index++; count++;
if(index == pattern.length()) index = 0;
}
}
return count;
}
}