ALGORITHM/백준

[JAVA] 백준 크로아티아 알파벳 - 2941

송경훈 2024. 5. 25. 13:25
반응형

📖 문제

 

☑️ 입출력 예제

 

알고리즘❓ 풀어내기❗️

다른 풀이 코드들을 보면 나와는 다르게 푼 게 대부분이었다. 내가 작성한 코드는 그렇게 깔끔한 코드는 아니지만 정말 단순하게 풀 수 있다. 풀이는 다음과 같다.
모든 크로아티아 알파벳을 각각의 새로운 변수에 대입해 준다. 그런 다음 입력받은 문자열을 이전에 크로아티아 알파벳으로 대입해 준 변수들로 하나씩 replace() 메서드를 사용하여 "a"로 대체해 준다. 이렇게 하고 나면 문자열은 a로만 이루어져 있을 것이고, 이 문자열의 길이가 답이 된다.

 

🧑🏻‍💻 풀이 코드

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String str1 = "c=";
        String str2 = "c-";
        String str3 = "dz=";
        String str4 = "d-";
        String str5 = "lj";
        String str6 = "nj";
        String str7 = "s=";
        String str8 = "z=";

        String newStr = str.replace(str1, "a");
        String newStr1 = newStr.replace(str2, "a");
        String newStr2 = newStr1.replace(str3, "a");
        String newStr3 = newStr2.replace(str4, "a");
        String newStr4 = newStr3.replace(str5, "a");
        String newStr5 = newStr4.replace(str6, "a");
        String newStr6 = newStr5.replace(str7, "a");
        String newStr7 = newStr6.replace(str8, "a");

        System.out.println(newStr7.length());
    }
}