728x90
반응형
SMALL
문제 설명
머쓱이는 태어난 지 6개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음을 최대 한 번씩 사용해 조합한(이어 붙인) 발음밖에 하지 못합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요.
제한사항
- 1 ≤ babbling의 길이 ≤ 100
- 1 ≤ babbling[i]의 길이 ≤ 15
- babbling의 각 문자열에서 "aya", "ye", "woo", "ma"는 각각 최대 한 번씩만 등장합니다.
- 즉, 각 문자열의 가능한 모든 부분 문자열 중에서 "aya", "ye", "woo", "ma"가 한 번씩만 등장합니다.
- 문자열은 알파벳 소문자로만 이루어져 있습니다.
입출력 예
babbling result
["aya", "yee", "u", "maa", "wyeoo"] | 1 |
["ayaye", "uuuma", "ye", "yemawoo", "ayaa"] | 3 |
입출력 예 설명
입출력 예 #1
- ["aya", "yee", "u", "maa", "wyeoo"]에서 발음할 수 있는 것은 "aya"뿐입니다. 따라서 1을 return합니다.
입출력 예 #2
- ["ayaye", "uuuma", "ye", "yemawoo", "ayaa"]에서 발음할 수 있는 것은 "aya" + "ye" = "ayaye", "ye", "ye" + "ma" + "woo" = "yemawoo"로 3개입니다. 따라서 3을 return합니다.
유의사항
- 네 가지를 붙여 만들 수 있는 발음 이외에는 어떤 발음도 할 수 없는 것으로 규정합니다. 예를 들어 "woowo"는 "woo"는 발음할 수 있지만 "wo"를 발음할 수 없기 때문에 할 수 없는 발음입니다.
- countMatches : 입력 문자열 **input**에서 주어진 정규식 **regex**와 일치하는 부분 문자열의 개수를 세는 역할
- 주어진 **regex**를 사용하여 Pattern 객체를 생성
- Pattern 객체의 splitAsStream 메소드를 사용하여 입력 문자열을 **regex**에 해당하는 부분 문자열을 기준으로 분할합니다. 이 메소드는 분할된 문자열을 스트림으로 반환
- count() 메소드를 사용하여 스트림의 요소 수를 세어 반환하고, 이때 1을 빼줌 이유는 **splitAsStream**이 반환하는 스트림의 요소 수는 실제 일치하는 부분 문자열의 개수보다 항상 1이 더 많기 때문
- "aya", "ye", "woo", "ma" 중 하나 이상의 발음이 등장하고 (ayaCount == 1 || yeCount == 1 || wooCount == 1 || maCount == 1)
- 네 가지 발음이 모두 합쳐 최대 4번 등장 (ayaCount + yeCount + wooCount + maCount <= 4)
import java.util.regex.Pattern; public class Solution { public int solution(String[] babbling) { int answer = 0; for (String word : babbling) { int ayaCount = countMatches(word, "aya"); int yeCount = countMatches(word, "ye"); int wooCount = countMatches(word, "woo"); int maCount = countMatches(word, "ma"); if ((ayaCount == 1 || yeCount == 1 || wooCount == 1 || maCount == 1) && (ayaCount + yeCount + wooCount + maCount <= 4)) { answer++; } } return answer; } public int countMatches(String input, String regex) { Pattern pattern = Pattern.compile(regex); return (int) pattern.splitAsStream(input).count() - 1; } }
어렵네여.. 어쩐지.. 정답률이 낮다 생각했더니.. ㅠㅠㅠㅠㅠ테스트 1입력값 〉["aya", "yee", "u", "maa", "wyeoo"]기댓값 〉1실행 결과 〉실행한 결괏값 3이 기댓값 1과 다릅니다.
다른 사람의 코드 - class Solution { public int solution(String[] babbling) { int answer = 0; for(int i =0; i < babbling.length; i++) { babbling[i] = babbling[i].replace("aya", "1"); babbling[i] = babbling[i].replace("woo", "1"); babbling[i] = babbling[i].replace("ye", "1"); babbling[i] = babbling[i].replace("ma", "1"); babbling[i] = babbling[i].replace("1", ""); if(babbling[i].isEmpty()) { answer = answer + 1; } } return answer; } }
- package Lv0; /* 머쓱이는 태어난 지 6개월 된 조카를 돌보고 있습니다. 조카는 아직 "aya", "ye", "woo", "ma" 네 가지 발음을 최대 한 번씩 사용해 조합한(이어 붙인) 발음밖에 하지 못합니다. 문자열 배열 babbling이 매개변수로 주어질 때, 머쓱이의 조카가 발음할 수 있는 단어의 개수를 return하도록 solution 함수를 완성해주세요. */ public class H120956 { public int solution(String[] babbling) { int answer = 0; for (String word : babbling) { if (isValidWord(word)) { answer++; } } return answer; } public boolean isValidWord(String word) { String[] pronunciations = {"aya", "ye", "woo", "ma"}; boolean[] used = new boolean[4]; int index = 0; for (int i = 0; i < word.length(); ) { boolean found = false; for (int j = 0; j < pronunciations.length; j++) { if (!used[j] && word.startsWith(pronunciations[j], i)) { used[j] = true; i += pronunciations[j].length(); found = true; break; } } if (!found) { return false; } } return true; } }
- import java.util.regex.Pattern; public class Solution { public int solution(String[] babbling) { int answer = 0; for (String word : babbling) { int ayaCount = countMatches(word, "aya"); int yeCount = countMatches(word, "ye"); int wooCount = countMatches(word, "woo"); int maCount = countMatches(word, "ma"); if (ayaCount <= 1 && yeCount <= 1 && wooCount <= 1 && maCount <= 1) { answer++; } } return answer; } public int countMatches(String input, String regex) { Pattern pattern = Pattern.compile(regex); return (int) pattern.splitAsStream(input).count() - 1; } }
- 여기서 "ayayemawoo"에 "aya"가 한 번 등장하므로, count() 메소드의 결과인 2에서 1을 빼준 값 1을 반환
728x90
반응형
LIST
'알고리즘 > 프로그래머스 JAVA LV.0' 카테고리의 다른 글
정수를 나선형으로 배치하기 (0) | 2023.05.07 |
---|---|
배열 조각하기 (0) | 2023.05.07 |
전국 대회 선발 고사 (0) | 2023.05.07 |
종이 자르기 (0) | 2023.05.07 |
왼쪽 오른쪽 (0) | 2023.05.07 |