프로그래머스 - kotlin/LEVEL 2
모음 사전
배준형
2022. 11. 25. 15:40
문제 출처: https://school.programmers.co.kr/learn/courses/30/lessons/92341?language=kotlin
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제
나의 풀이
이 문제는 순열 완전탐색을 이용해서 깊이 우선탐색으로 구현하였다. 알파벳 모음을 만들면서 word에 해당하는 알파벳이 나오면 탐색을 그만하고 count 값을 출력해주는 식으로 구현하였다.
class Solution {
var count = 0
val alphabet = "AEIOU".toList()
val answer = ArrayList<String>()
var find = false
fun permutation(depth: Int, next: String, word: String){
if(depth == 6) return
if(next == word) {
find = true
return
}
for (i in alphabet) {
if(find) return
if(depth + 1 < 6) count++
permutation(depth + 1, next.plus(i), word)
}
}
fun solution(word: String): Int {
permutation(0, "", word)
return count
}
}