정답 참고 후 내가 푼 풀이
class Solution {
String[] babyBabbling = {"aya", "ye", "woo", "ma"};
String[] noBabbling = {"ayaaya", "yeye", "woowoo", "mama"};
public int solution(String[] babbling) {
int answer = 0;
out:
for (String str : babbling) {
String newStr = str;
int idx = 0;
for (String babble : babyBabbling) {
newStr = newStr.replace(babble, "" + idx);
idx++;
}
for (int i = 0; i < newStr.length() - 1; i++) {
char ch = newStr.charAt(i);
if (!Character.isDigit(ch) || ch == newStr.charAt(i + 1)) {
continue out;
}
}
if (!Character.isDigit(newStr.charAt(newStr.length() - 1))) {
continue out;
}
answer++;
}
return answer;
}
}
다른 사람이 푼 풀이
class Solution {
String[] babyBabbling = {"aya", "ye", "woo", "ma"};
String[] noBabbling = {"ayaaya", "yeye", "woowoo", "mama"};
public int solution(String[] babbling) {
int answer = 0;
out:
for (String str : babbling) {
String newStr = str;
for (String s : noBabbling) {
if (newStr.contains(s)) {
continue out;
}
}
for (String s : babyBabbling) {
newStr = newStr.replace(s, " ");
}
if (newStr.trim().length() == 0) {
answer++;
}
}
return answer;
}
}