본문 바로가기

프로그래머스/lv1

옹알이 (2)

/* 옹알이 (2)
 * 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음과
 * 네 가지 발음을 조합해서 만들 수 있는 발음밖에 하지 못하고
 * 연속해서 같은 발음을 하는 것을 어려워합니다.
 * 문자열 배열 babbling이 매개변수로 주어질 때,
 * 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return
 *
 * babbling                                         result
 * ["aya", "yee", "u", "maa"]                       1      
 * 발음할 수 있는 것은 "aya"뿐입니다. 따라서 1을 return
 * ["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]   2
 * 발음할 수 있는 것은 "aya" + "ye" = "ayaye", "ye" + "ma" + "woo" = "yemawoo"로 2개
 * "yeye"는 같은 발음이 연속되므로 발음할 수 없습니다.
 */
 
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
public class programmer_1_6 {
    static String[] a1 = {"aya""yee""u""maa"};
    static String[] a2 = {"wooyemawooye"};
    public int solution(String[] babbling) {
        int answer = 0;
        String[] can = {"aya""ye""woo""ma"};
        String[] cant = {"ayaaya""yeye""woowoo""mama"};
        for(int i = 0; i < babbling.length; i++){
            for(int k = 0; k < can.length; k++){
                babbling[i] = babbling[i].replace(cant[k], "2");
                babbling[i] = babbling[i].replace(can[k], "1");
            }
        }
        for(String i : babbling){
            if(!i.equals(""&& i.replaceAll("1""").length() == 0) answer++;
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_1_6 t = new programmer_1_6();
        // System.out.println("---------------------------------------");
        // System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
        // System.out.println("result3 = " + t.solution(a3));
        // System.out.println("---------------------------------------");
    }
}
cs

>> wooyemawooye 해당 반례 신경써야함..

woo ye ma woo ye 

초기 방식인 replaceFirst로 진행시, 연속된 발음에 대해서는 거를 수 있으나 다음 반례처럼 중간중간 다른 것이 섞인 것에 대해서는 처리가 불가.

'프로그래머스 > lv1' 카테고리의 다른 글

약수의 합  (0) 2022.12.08
짝수와 홀수  (0) 2022.12.08
문자열 나누기  (0) 2022.12.07
푸드 파이트 대회  (0) 2022.12.07
과일 장수  (0) 2022.12.07