Light Blue Pointer
본문 바로가기
Coding Test

[프로그래머스]옹알이

by Greedy 2023. 12. 11.

문제 주소

https://school.programmers.co.kr/learn/courses/30/lessons/133499

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제 설명

머쓱이는 태어난 지 11개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음과 네 가지 발음을 조합해서 만들 수 있는 발음밖에 하지 못하고 연속해서 같은 발음을 하는 것을 어려워합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.

제한사항

  • 1 ≤ babbling의 길이 ≤ 100
  • 1 ≤ babbling[i]의 길이 ≤ 30
  • 문자열은 알파벳 소문자로만 이루어져 있습니다.

입출력 예

babbling result

["aya", "yee", "u", "maa"] 1
["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"] 2

입출력 예 설명

입출력 예 #1

  • ["aya", "yee", "u", "maa"]에서 발음할 수 있는 것은 "aya"뿐입니다. 따라서 1을 return합니다.

입출력 예 #2

  • ["ayaye", "uuuma", "yeye", "yemawoo", "ayaayaa"]에서 발음할 수 있는 것은 "aya" + "ye" = "ayaye", "ye" + "ma" + "woo" = "yemawoo"로 2개입니다. "yeye"는 같은 발음이 연속되므로 발음할 수 없습니다. 따라서 2를 return합니다.

유의사항

  • 네 가지를 붙여 만들 수 있는 발음 이외에는 어떤 발음도 할 수 없는 것으로 규정합니다. 예를 들어 "woowo"는 "woo"는 발음할 수 있지만 "wo"를 발음할 수 없기 때문에 할 수 없는 발음입니다.

풀이과정

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            int size = s.length();
            for(String a : able){
                s.replace(a,"");
                if(size - s.length()>0){
                    answer += (size - s.length())/a.length();
                }
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 0이 기댓값 1과 다릅니다.
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 0이 기댓값 2과 다릅니다.

왜 안되지….

로그 찍어봄

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            int size = s.length();
            System.out.println(s);
            for(String a : able){
                System.out.println(a);
                s.replace(a,"");
                System.out.println(s);
                if(size - s.length()>0){
                    answer += (size - s.length())/a.length();
                }
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 0이 기댓값 1과 다릅니다.
출력 〉	
aya
aya
aya -> ""만 나와야되는데요??
ye
aya
woo
aya
ma
aya
yee
aya
yee
ye
yee
woo
yee
ma
yee
u
aya
u
ye
u
woo
u
ma
u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 0이 기댓값 2과 다릅니다.
출력 〉	ayaye
aya
ayaye
ye
ayaye
woo
ayaye
ma
ayaye
uuu
aya
uuu
ye
uuu
woo
uuu
ma
uuu
yeye
aya
yeye
ye
yeye
woo
yeye
ma
yeye
yemawoo
aya
yemawoo
ye
yemawoo
woo
yemawoo
ma
yemawoo
ayaayaa
aya
ayaayaa
ye
ayaayaa
woo
ayaayaa
ma
ayaayaa

파라미터로 오는건 읽기전용이라 수정이 안 되나? 싶어서 deep copy해봄

import java.util.Arrays;
class Solution {
    public int solution(String[] babbling) {
        String [] babblingCopy = Arrays.clone(babbling);
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babblingCopy){
            int size = s.length();
            System.out.println(s);
            for(String a : able){
                System.out.println(a);
                s.replace(a,"");
                System.out.println(s);
                if(size - s.length()>0){
                    answer += (size - s.length())/a.length();
                }
            }
        }
        return answer;
    }
}
/Solution.java:4: error: method clone in class Object cannot be applied to given types;
        String [] babblingCopy = Arrays.clone(babbling);
                                       ^

String [] babblingCopy = Arrays.clone(babbling);

String [] babblingCopy = babbling.clone();

import java.util.Arrays;
class Solution {
    public int solution(String[] babbling) {
        String [] babblingCopy = babbling.clone();
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babblingCopy){
            int size = s.length();
            System.out.println(s);
            for(String a : able){
                System.out.println(a);
                s.replace(a,"");
                System.out.println(s);
                if(size - s.length()>0){
                    answer += (size - s.length())/a.length();
                }
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 0이 기댓값 1과 다릅니다.
출력 〉	aya
aya
aya
ye
aya
woo
aya
ma
aya
yee
aya
yee
ye
yee
woo
yee
ma
yee
u
aya
u
ye
u
woo
u
ma
u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 0이 기댓값 2과 다릅니다.
출력 〉	ayaye
aya
ayaye
ye
ayaye
woo
ayaye
ma
ayaye
uuu
aya
uuu
ye
uuu
woo
uuu
ma
uuu
yeye
aya
yeye
ye
yeye
woo
yeye
ma
yeye
yemawoo
aya
yemawoo
ye
yemawoo
woo
yemawoo
ma
yemawoo
ayaayaa
aya
ayaayaa
ye
ayaayaa
woo
ayaayaa
ma
ayaayaa

응 그래봤자 replace가 안됨~~

무궁화 삼천리 화려강산 대한사람 대한으로 길이 보전하세 ";	
//replaceAll([정규식],[바꿀문자])
a= a.replaceAll("대한", "민국");
System.out.println(a);

String a = "무궁화. 삼천리. 화려강산. 대한사람. 대한으로. 길이. 보전하세 ";
//replace([기존문자],[바꿀문자])
a = a.replace(".", "/");
System.out.println(a);

//결과값 : 무궁화/ 삼천리/ 화려강산/ 대한사람/ 대한으로/ 길이/ 보전하세

String a = "무궁화. 삼천리. 화려강산. 대한사람. 대한으로. 길이. 보전하세 ";
//replaceAll([정규식],[바꿀문자])
a = a.replaceAll(".", "/");
System.out.println(a);

//결과값 : /////////////////////////////////////

for문 안에 들어가는것도 읽기전용인가 싶어서 새로운 String 으로 받아봄

import java.util.Arrays;
class Solution {
    public int solution(String[] babbling) {
        String [] babblingCopy = babbling.clone();
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babblingCopy){
            int size = s.length();
            System.out.println(s);
            for(String a : able){
                System.out.println(a);
                String replacedS = s.replace(a,"");//-> 왜 안돼 
                System.out.println(replacedS);
                if(size - replacedS.length()>0){
                    answer += (size - replacedS.length())/a.length();
                }
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 2이 기댓값 1과 다릅니다.
출력 〉	aya
aya

ye
aya
woo
aya
ma
aya
yee
aya
yee
ye
e
woo
yee
ma
yee
u
aya
u
ye
u
woo
u
ma
u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 9이 기댓값 2과 다릅니다.
출력 〉	ayaye
aya
ye
ye
aya
woo
ayaye
ma
ayaye
uuu
aya
uuu
ye
uuu
woo
uuu
ma
uuu
yeye
aya
yeye
ye

woo
yeye
ma
yeye
yemawoo
aya
yemawoo
ye
mawoo
woo
yema
ma
yewoo
ayaayaa
aya
a
ye
ayaayaa
woo
ayaayaa
ma
ayaayaa

오 이제 뭐가 된다

babbling copy는 안 하는걸로

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            int size = s.length();
            System.out.println("size " + size);
            System.out.println("s " + s);
            String replacedS = s;
            for(String a : able){
                System.out.println("a " + a);
                System.out.println(a);
                replacedS = replacedS.replace(a,"");
                if(size - replacedS.length()>0){
                    System.out.println("answer "+(size - replacedS.length())/a.length());
                    answer = answer + ((size - replacedS.length())/a.length());
                }
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 6이 기댓값 1과 다릅니다.
출력 〉	
size 3
s aya
a aya
aya
answer 1

a ye
ye
answer 1
a woo
woo
answer 1
a ma
ma
answer 1
size 3
s yee
a aya
aya
a ye
ye
answer 1
a woo
woo
answer 0
a ma
ma
answer 1
size 1
s u
a aya
aya
a ye
ye
a woo
woo
a ma
ma
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 26이 기댓값 2과 다릅니다.
출력 〉	size 5
s ayaye
a aya
aya
answer 1
a ye
ye
answer 2
a woo
woo
answer 1
a ma
ma
answer 2
size 3
s uuu
a aya
aya
a ye
ye
a woo
woo
a ma
ma
size 4
s yeye
a aya
aya
a ye
ye
answer 2
a woo
woo
answer 1
a ma
ma
answer 2
size 7
s yemawoo
a aya
aya
a ye
ye
answer 1
a woo
woo
answer 1
a ma
ma
answer 3
size 7
s ayaayaa
a aya
aya
answer 2
a ye
ye
answer 3
a woo
woo
answer 2
a ma
ma
answer 3

아 근데 생각해보니 내가 짠 코드는 woo 를 읽을 수 있을때 wooo도 읽을 수 있잖아 굳이 저렇게 안 하고 걍 equals쓰면 될걸 왜 저러고 있지

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            for(String a : able){
                if(s.equals(a)){
                    answer ++;
                }
            }
        }
        return answer;
    }
}

왜냐면 포함된것도 읽긴 읽으니까…

문제를 똑바로 파악하고 시작해야 시간이 덜 걸릴듯

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            int size = s.length();
            String replacedS = s;
            for(String a : able){
                replacedS = replacedS.replace(a,"");
            }
            if(replacedS.length()==0){
                answer++;
            }
        }
        return answer;
    }
}

1은 통과 2는 안됨

로그찍어봄

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            int size = s.length();
            System.out.println("s " + s);
            String replacedS = s;
            for(String a : able){
                replacedS = replacedS.replace(a,"");
            }
            //문제를 똑바로좀 읽고 시작해라 ㅇ?
            if(replacedS.length()==0){
                answer++;
                System.out.println("answer " + answer);
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	테스트를 통과하였습니다.
출력 〉	
s aya
answer 1
s yee
s u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 3이 기댓값 2과 다릅니다.
출력 〉	
s ayaye
answer 1
s uuu
s yeye
answer 2
s yemawoo
answer 3
s ayaayaa

"yeye"는 같은 발음이 연속되므로 발음할 수 없습니다.

→ replace가 아니라 replaceFirst를 써야할듯

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            int size = s.length();
            System.out.println("s " + s);
            String replacedS = s;
            for(String a : able){
                replacedS = replacedS.replaceFirst(a,"");
            }
            if(replacedS.length()==0){
                answer++;
                System.out.println("answer " + answer);
            }
        }
        return answer;
    }
}

테스트케이스는 통과함

정확성  테스트
테스트 1 〉	실패 (6.89ms, 75.6MB)
테스트 2 〉	통과 (12.13ms, 76.9MB)
테스트 3 〉	통과 (7.32ms, 80.7MB)
테스트 4 〉	통과 (9.59ms, 64.7MB)
테스트 5 〉	통과 (6.75ms, 73.8MB)
테스트 6 〉	통과 (8.83ms, 79.4MB)
테스트 7 〉	통과 (9.66ms, 73.6MB)
테스트 8 〉	통과 (7.64ms, 81.7MB)
테스트 9 〉	실패 (10.42ms, 77.4MB)
테스트 10 〉	실패 (11.76ms, 75.8MB)
테스트 11 〉	실패 (8.53ms, 78MB)
테스트 12 〉	통과 (9.77ms, 74.8MB)
테스트 13 〉	통과 (9.86ms, 79.5MB)
테스트 14 〉	실패 (16.82ms, 72.5MB)
테스트 15 〉	통과 (10.58ms, 78.7MB)
테스트 16 〉	실패 (12.50ms, 82.3MB)
테스트 17 〉	실패 (7.66ms, 79.2MB)
테스트 18 〉	통과 (17.31ms, 86.8MB)
테스트 19 〉	실패 (16.49ms, 70.5MB)
테스트 20 〉	실패 (9.98ms, 74.7MB)

생각해보니 같은 단어가 연속된다고 했지 두번 나오면 안 된다 소리는 안 했음

응 이런방식으론 못풀어 ~

걍 한글자씩 읽어야할듯

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        String ex = "";
        for(String s : babbling){
            String reading  = "";
            System.out.println("s " + s);
            for(int i = 0 ;i<s.length();i++){
                reading += s.charAt(i);
                System.out.println("reading " + reading);
                for(String a : able){
                    replace(a,"")
                    if(reading.equals(a)){
                        if(!(reading.equals(ex))){
                            answer++;
                            reading = "";
                            System.out.println("answer " + answer);
                        }
                    ex = a;
                    }
                }
            }              
        }
     return answer;
     }
}

하다가 생각해보니 지금 reading을 비워줘야되는데 안 비워주고 있음

생각해보니 지금 reading 안에 다른 글자로 시작해서 발음할 수 있는 단어가 포함되어서 나오면 안 세고 있는듯

reading이 a랑 equal 하냐가 아니고 a에 포함되었는지 봐야 할듯

지금생각해보니 replace를 “”가 아니고 이상한 문자로 해서 개수 세면 되잖아..?

두번나오면 안 되는 걸로 count안 하고

String contains가 있나? 개수도 세주면 좋을듯

Java String contains() Method

String myStr = "Hello";
System.out.println(myStr.contains("Hel"));   // true
System.out.println(myStr.contains("e"));     // true
System.out.println(myStr.contains("Hi"));    // false
class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            String str = s;
            for(String a : able){
                int size = str.length();
                str = str.replace(a,"$");
                if(str.contains("$$")){
                    str.replace("$","");
                }else if(str.contains("$")){
                    str.replace("$","");
                    answer += ((size - str.length())/a.length());
                }
            }
        }
        return answer;
    }
}
class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            System.out.println("s "+s);
            String str = s;
            for(String a : able){
                int size = str.length();
                str = str.replace(a,"$");
                System.out.println("before str "+str);
                if(str.contains("$$")){
                    System.out.println("contains $$");
                    str.replace("$","");
                }
                if(str.contains("$")){
                    System.out.println("contains $");
                    str.replace("$","");
                    answer = answer + ((size - str.length())/a.length());
                    System.out.println("answer "+answer);
                }
                System.out.println("after str "+str);
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 0이 기댓값 1과 다릅니다.
출력 〉	
s aya
before str $
contains $
answer 0 -> contains $인데 answer은 왜 0이야 
after str $
before str $
contains $
answer 0
after str $
before str $
contains $
answer 0
after str $
before str $
contains $
answer 0
after str $
s yee
before str yee
after str yee
before str $e
contains $
answer 0
after str $e
before str $e
contains $
answer 0
after str $e
before str $e
contains $
answer 0
after str $e
s u
before str u
after str u
before str u
after str u
before str u
after str u
before str u
after str u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	테스트를 통과하였습니다.
출력 〉	s ayaye
before str $ye
contains $
answer 0
after str $ye
before str $$
contains $$
contains $
answer 0
after str $$
before str $$
contains $$
contains $
answer 0
after str $$
before str $$
contains $$
contains $
answer 0
after str $$
s uuu
before str uuu
after str uuu
before str uuu
after str uuu
before str uuu
after str uuu
before str uuu
after str uuu
s yeye
before str yeye
after str yeye
before str $$
contains $$
contains $
answer 1
after str $$
before str $$
contains $$
contains $
answer 1
after str $$
before str $$
contains $$
contains $
answer 1
after str $$
s yemawoo
before str yemawoo
after str yemawoo
before str $mawoo
contains $
answer 1
after str $mawoo
before str $ma$
contains $
answer 1
after str $ma$
before str $$$
contains $$
contains $
answer 1
after str $$$
s ayaayaa
before str $$a
contains $$
contains $
answer 2
after str $$a
before str $$a
contains $$
contains $
answer 2
after str $$a
before str $$a
contains $$
contains $
answer 2
after str $$a
before str $$a
contains $$
contains $
answer 2
after str $$a

(size - str.length())/a.length())

이부분이 잘못된 거 같다

0이 나왔음

    		        int size = str.length();
                if(str.contains("$")){
                    System.out.println("contains $");
                    str.replace("$","");
                    System.out.println(((size - str.length())/a.length()));
                    answer = answer + (size - str.length());

이렇게 수정함

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            System.out.println("s "+s);
            String str = s;
            for(String a : able){
                str = str.replace(a,"$");
                System.out.println("before str "+str);
                if(str.contains("$$")){
                    System.out.println("contains $$");
                    str.replace("$","");
                }
                int size = str.length();
                if(str.contains("$")){
                    System.out.println("contains $");
                    str.replace("$","");
                    System.out.println(size - str.length());
                    answer = answer + (size - str.length());
                    System.out.println("answer "+answer);
                }
                System.out.println("after str "+str);
            }
        }
        return answer;
    }
}
테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 0이 기댓값 1과 다릅니다.
출력 〉	
s aya
before str $
contains $
0 -> 안들어가고
answer 0
after str $ -> replace도 안됐나본데?
before str $
contains $
0

도대체 왜 0만 나옴….

$를 두글재로 대체하고 늘어난 길이를 재보자 ㅎ

“”도 1글자로 인식해서 저러나…

s aya

before str $

contains $

0

answer 0

after str $

돌겟습니다

길이가 이상해서 찍어봄

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            System.out.println("s "+s);
            String str = s;
            for(String a : able){
                str = str.replace(a,"$");
                if(str.contains("$$")){
                    str.replace("$","");
                }
                int size = str.length();
                System.out.println("size "+ size);
                if(str.contains("$")){
                    str.replace("$","%%");
                    System.out.println("str.length() "+ str.length());
                    answer = answer + (str.length()-size);
                }
            }
        }
        return answer;
    }
}

s aya

size 1

str.length() 1

도대체 왜 그럼?

아 이거 또 그거다 shallow copy 때문에 이러는거 ㅋㅋㅋㅋ….

⚠️String shallow copy에 주의

⭐Java deep copy → 이거 아니었음

String s = "hello";
String copy = new String(s);
class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            System.out.println("s "+s);
            String str = new String(s);
            for(String a : able){
                str = str.replace(a,"$");
                if(str.contains("$$")){
                    str.replace("$","");
                }                
                if(str.contains("$")){
                    int size = str.length();
                    System.out.println("size "+ size);
                    str.replace("$","%%");//->
                    System.out.println("str.length() "+ str.length());
                    answer = answer + (str.length()-size);
                }
            }
        }
        return answer;
    }
}

그것도 아닌가 본데요

디버깅 str.replace →

테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 0이 기댓값 1과 다릅니다.
출력 〉	
s aya
size 1
str.length() 1 -> 왜 2가 안 되냐고???
size 1
str.length() 1
size 1
str.length() 1
size 1
str.length() 1
s yee
size 2
str.length() 2
size 2
str.length() 2
size 2
str.length() 2
s u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 0이 기댓값 2과 다릅니다.
출력 〉	
s ayaye
size 3
str.length() 3
size 2
str.length() 2
size 2
str.length() 2
size 2
str.length() 2
s uuu
s yeye
size 2
str.length() 2
size 2
str.length() 2
size 2
str.length() 2
s yemawoo
size 6
str.length() 6
size 4
str.length() 4
size 3
str.length() 3
s ayaayaa
size 3
str.length() 3
size 3
str.length() 3
size 3
str.length() 3
size 3
str.length() 3

 

str.replace("$","%%");//-> 이 부분이 안 돼서 개삽질했는데

 

str = str.replace("$","%%");

이렇게 str에다 넣어줘야 되는 거였음 ㅋㅋㅋㅋㅋㅋㅋ

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            System.out.println("s "+s);
            String str = new String(s);
            for(String a : able){
                str = str.replace(a,"$");
                if(str.contains("$$")){
                    str = str.replace("$","");
                }                
                if(str.contains("$")){
                    int size = str.length();
                    System.out.println("size "+ size);
                    System.out.println("str "+ str);
                    str = str.replace("$","%%");
                    
                    System.out.println("str "+ str);
                    System.out.println("str.length() "+ str.length());
                    answer = answer + (str.length()-size);
                    System.out.println("answer "+ answer);
                }
            }
        }
        return answer;
    }
}

s aya

size 1

str $

str %%

 

 

str.length() 2

answer 1

s yee

size 2

str $e

str %%e

str.length() 3

answer 2

s u

→ ye e일때 ye가 아니라서 발음을 못하나봄…

$가 있을때 뒤에 붙은게 자음이 아니면 안 되게 해야하나…


⭐java matches consonant

match a-z except [a,e,i,o,u]

[a-z&&[^aeiou]]
class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        for(String s : babbling){
            System.out.println("s "+s);
            String str = new String(s);
            for(String a : able){
                str = str.replace(a,"$"); 
                if(str.contains("$$")){
                    str = str.replace("$","");
                }                
                if(str.contains("$")){
                    int size = str.length();
                    str = str.replace("$","");
                    if((str.matches("[a-z&&[^aeiou]]"))&(str.length()>0)){
                        
                    }else{
                        answer = answer + (size-str.length());
                        System.out.println("answer "+ answer);
                    }
                }
            }
        }
        return answer;
    }
}

yemawoo를 하나씩 다 세고있잖아요

테스트 1
입력값 〉	["aya", "yee", "u"]
기댓값 〉	1
실행 결과 〉	실행한 결괏값 2이 기댓값 1과 다릅니다.
출력 〉	s aya
answer 1
s yee
answer 2
s u
테스트 2
입력값 〉	["ayaye", "uuu", "yeye", "yemawoo", "ayaayaa"]
기댓값 〉	2
실행 결과 〉	실행한 결괏값 5이 기댓값 2과 다릅니다.
출력 〉	s ayaye
answer 1
answer 2
s uuu
s yeye
s yemawoo
answer 3
answer 4
answer 5
s ayaayaa

여전히 yeye이런것도 세고있음 ㅜ

생각해보니 $$ 검증과정을 좀 바꾸고?

아래에서 a-z 매치 쓰면 될거같음

걍 숫자로 바꾸자 ㅎ

if(str.contains("$$")){
                    str = str.replace("$","");
                }                
                if(str.contains("$")){
                    int size = str.length();
                    str = str.replace("$","");
                    if((str.matches("[a-z&&[^aeiou]]"))&(str.length()>0)){
                        
                    }else{
                        answer = answer + (size-str.length());
                        System.out.println("answer "+ answer);
                    }
                }
class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        Loop1:
        for(String s : babbling){
            System.out.println("s "+s);
            String str = new String(s);
            for(int i=0; i<able.length; i++ ){
                str = str.replace(able[i],Integer.toString(i));
                if(str.contains(i+""+i)){
                    continue Loop1;
                }
            }
            str = str.replaceAll("[0-9]","");
            if(str.length()==0){
                answer++;
            }
        }
        return answer;
    }
}

아 드디어 풀렸다

 

제출 코드

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String [] able = {"aya", "ye", "woo", "ma"};
        Loop1:
        for(String s : babbling){
            String str = new String(s);
            for(int i=0; i<able.length; i++ ){
                str = str.replace(able[i],Integer.toString(i));
                if(str.contains(i+""+i)){
                    continue Loop1;
                }
            }
            str = str.replaceAll("[0-9]","");
            if(str.length()==0){
                answer++;
            }
        }
        return answer;
    }
}