해당 스트링을 공백을 기준으로 자른후,사전과 비교해 가장 짧은 startsWith로 변경
사전을 정렬하고,find를 쓰면될듯
정답
class Solution {
fun replaceWords(dictionary: List<String>, sentence: String): String {
val sortDict = dictionary.sorted()
val splitSentence = sentence.split(' ')
var res=""
for (i in splitSentence){
val temp = sortDict.find { i.startsWith(it) } ?: i
res+= "$temp "
}
return res.trim()
}
}
'알고리즘' 카테고리의 다른 글
릿코드 200일 달성 (0) | 2024.05.27 |
---|---|
릿코드 1863. Sum of All Subset XOR Totals 코틀린 (0) | 2024.05.20 |
릿코드 1325. Delete Leaves With a Given Value 코틀린 (0) | 2024.05.17 |
릿코드 2373. Largest Local Values in a Matrix 코틀린 (0) | 2024.05.12 |
릿코드 786. K-th Smallest Prime Fraction 코틀린 (0) | 2024.05.10 |