반응형
📖 문제
☑️ 입출력 예제
알고리즘❓ 풀어내기❗️
다른 풀이 코드들을 보면 나와는 다르게 푼 게 대부분이었다. 내가 작성한 코드는 그렇게 깔끔한 코드는 아니지만 정말 단순하게 풀 수 있다. 풀이는 다음과 같다.
모든 크로아티아 알파벳을 각각의 새로운 변수에 대입해 준다. 그런 다음 입력받은 문자열을 이전에 크로아티아 알파벳으로 대입해 준 변수들로 하나씩 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());
}
}
'ALGORITHM > 백준' 카테고리의 다른 글
[JAVA] 백준 부분수열의 합 - 1182 (0) | 2024.07.30 |
---|---|
[JAVA] 백준 킹, 퀸, 룩, 비숍, 나이트, 폰 - 3003 (0) | 2024.05.25 |
[JAVA] 백준 팰린드롬인지 확인하기 - 10988 (0) | 2024.05.25 |
[JAVA] 백준 별 찍기 7 -2744 (0) | 2024.05.25 |
[JAVA] 백준 1546 - 평균 (0) | 2024.05.19 |