반응형
https://school.programmers.co.kr/learn/courses/30/lessons/120956
1. 조카는 4가지 단어만 발음할 수 있으므로 해당 단어들을 리스트로 만들어준다
val words = listOf("aya", "ye", "woo", "ma")
2. 주어진 단어들 babbling 리스트를 map으로 순회하면서 발음 할 수 있는 단어들을 지워준다
이때, 중간 단어가 사라지며 남은 문자들끼리 새 단어로 조합될 수 있으므로 "," 로 변환하여 새 단어가 만들어지지 않도록 한다!
ex) wyeoo -> ye를 없애면 woo 가 만들어져서 지워질 수 있다
babbling.map { str ->
words.fold(str) { acc, word->
acc.replace(word, ",")
}
}
3. 2에서 반환된 문자리스트에서 "," 를 다시 지워주고 빈 문자열을 카운트 한다
.count{ it
.replace(",","")
.isEmpty()
}
[전체코드]
class Solution {
fun solution(babbling: Array<String>): Int {
val words = listOf("aya", "ye", "woo", "ma")
return babbling.map { str ->
words.fold(str) { acc, word->
acc.replace(word, ",")
}
}.count{ it.replace(",","").isEmpty() }
}
}
반응형
'Online-Judge > Programmers' 카테고리의 다른 글
[Programmers] Python 약수의 합 (0) | 2022.07.17 |
---|---|
[Programmers] Python 크레인 인형뽑기 게임 (0) | 2022.07.17 |
[Programmers] Python 나누어 떨어지는 숫자 배열 (0) | 2022.07.17 |
[Programmers] Python 문자열 내림차순으로 배치하기 (0) | 2022.07.17 |
[Programmers] Python 오픈채팅방 (0) | 2022.07.17 |
댓글