Just another easy problem.
class Solution {
fun findTheDifference(s: String, t: String): Char {
var chars = IntArray(26)
t.forEach {
chars[it - 'a']++
}
s.forEach {
chars[it - 'a']--
}
chars.forEachIndexed { index, value ->
if(value > 0) return (index+97).toChar()
}
throw IllegalStateException("Should not happen")
}
}
Runtime: 156 ms, faster than 86.54% of Kotlin online submissions for Find the Difference.
Memory Usage: 32.7 MB, less than 100.00% of Kotlin online submissions for Find the Difference.
The idea is to have a character histogram of t
and then for each character of s
, reduce the matching bins in the histogram. Any bin whose count is not zero represents the added character.