본문 바로가기

알고리즘

릿코드 1347. Minimum Number of Steps to Make Two Strings Anagram 코틀린

문자열2개를 해시맵에 넣은다음,t에서 s를 빼고 남은수리턴

정답

class Solution {
    fun minSteps(s: String, t: String): Int {
        val sHashMap = hashMapOf<Char, Int>()
        val tHashMap = hashMapOf<Char, Int>()
        for (i in s){
            sHashMap[i]=sHashMap[i]?.plus(1)?:1
        }
        for (i in t){
            tHashMap[i]=tHashMap[i]?.plus(1)?:1
        }
        for ((key,value) in sHashMap){
            if (tHashMap[key]==null){
                continue
            }
            tHashMap[key]=tHashMap[key]!!- value
        }

        return tHashMap.filter { it.value>0 }.values.sum()
    }
}