우선 그냥 내가 떠올린 노가다 방법이다. if문 대신 switch문을 사용했고 인덱스 범위 안에 있는지 꼭 확인하고, and과 or 연산자 사이에서 괄호에 주의하자!!(엄청난 런타임 에러를 겪음ㅎ)
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int count=0;
int len = str.length();
for(int i=0; i<len; i++){
count++;
switch (str.charAt(i)) {
case 'c':
if(i < len-1 && (str.charAt(i+1) == '-' || str.charAt(i+1) == '=')){
i++;
}
break;
case 'd':
if (i < len-2 && str.charAt(i+1) == 'z' && str.charAt(i+2) == '='){
i+=2;
}
else if (i < len-1 && str.charAt(i+1) == '-'){
i++;
}
break;
case 'l':
case 'n':
if(i < len-1 && str.charAt(i+1) == 'j'){
i++;
}
break;
case 's':
case 'z':
if(i < len-1 && str.charAt(i+1) == '='){
i++;
}
break;
}
}
System.out.println(count);
}
}
분명 다른 간단한 방법이 있을 것 같아서 구글링해서 찾은 방법이다.
크로아티아 알파벳 자체를 한 개의 알파벳으로 대체한 후, 전체 길이를 구함으로써 알파벳 개수를 찾는 방법이다.
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
String[] arr = { "c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z=" };
for(String val : arr) {
str = str.replace(val, "A");
}
System.out.println(str.length());
}
}

엄청난 성능차이까진 아니군 ㅎ
그치만 replace 함수 하나 알아간다!