2941번 크로아티아 알파벳

Day6 6단계 20231024


풀이 1

  1. 내 아이디어

    • 각 시작 알파벳 다음으로 올 수 있는 문자가 어느 것이 있는지 체크해서 조건문으로 판별해보자
    • d의 경우엔 z=가 오는지, -가 오는지 확인해야 한다.
  2. 다른 사람의 아이디어

    • https://st-lab.tistory.com/68
    • String.charAt(i)를 써서 String을 배열화 하지 않고 각 인덱스 위치의 char를 읽어와서 조건별로 나누어 판단.
    • 비교 시 인덱스가 범위를 초과하지 않도록 판별 문자에 따라 (length() -1) 혹은 (length()-2) 위치까지만 순환 시켰다.
import java.io.*;
import java.util.*;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String cro = br.readLine();
		char[][] croRefer = {{'z', '='}, {'s', '='}, {'n', 'j'}, {'l', 'j'}, {'d', '-'},
							 {'c', '='}, {'c', '-'}};
		int twoCount = 0;
		int ThreeCount = 0;
		pass : for (int i = 0; i < cro.length()-1; i++) {
			for (int j = 0; j < croRefer.length; j++) {
				if(cro.charAt(i) == croRefer[j][0] && cro.charAt(i+1) == croRefer[j][1]) {
					twoCount++;
					i++;		
					continue pass;
				} else if(i < cro.length()-2 && cro.charAt(i) == 'd' 
						&& cro.charAt(i+1) == 'z' && cro.charAt(i+2) == '=') {
					ThreeCount++;
					i += 2;
					continue pass;
				}
			}
		}
		System.out.println(cro.length()-twoCount-2*ThreeCount);
		br.close();
	}
}

풀이 2

import java.io.*;

public class Main {
    public static void main(String[] args) {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            String[] alphabet = {"c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z="};
            String line = reader.readLine();
            for (int i = 0; i < alphabet.length; i++) {
                if (line.contains(alphabet[i])) {
                    line = line.replaceAll(alphabet[i], "*");
                }
            }
            System.out.println(line.length());
        } catch (Exception e) {
        }
    }
}