아나그램인지 확인해서 걔들끼리 그룹묶어서 리턴
그냥 해시맵의 키에 정렬된리스트를(set은 중복제거때문에 사용불가하고,순서를 통일해야 해시맵에서 확인이되니 정렬사용) 쓰고,
같은거끼리 인덱스묶어두고,그거 전부 묶어서 리턴하자
정답
class Solution {
fun groupAnagrams(strs: Array<String>): List<List<String>> {
val strHashMap = hashMapOf<List<Char>, List<Int>>()
for ((index,value) in strs.withIndex()){
val tempSet = value.toList().sorted()
strHashMap[tempSet]=strHashMap[tempSet]?.plus(index)?: listOf(index)
}
val resList= mutableListOf<MutableList<String>>()
for ((index,value) in strHashMap){
val tempList= mutableListOf<String>()
for (i in value){
tempList.add(strs[i])
}
resList.add(tempList)
}
return resList
}
}
'알고리즘' 카테고리의 다른 글
릿코드 169. Majority Element 코틀린 (0) | 2024.02.12 |
---|---|
릿코드 451. Sort Characters By Frequency 코틀린 (0) | 2024.02.07 |
릿코드 387. First Unique Character in a String 코틀린 (0) | 2024.02.05 |
릿코드 1291. Sequential Digits 코틀린 (0) | 2024.02.02 |
릿코드 2966. Divide Array Into Arrays With Max Difference 코틀린 (0) | 2024.02.01 |