Spot the Difference
1 min read

Spot the Difference

You are given two strings, s and t which only consist of lowercase letters. t is generated by shuffling the letters in s as well as potentially adding an additional random character. Return the letter that was randomly added to t if it exists, otherwise, return ’  ‘.
Note: You may assume that at most one additional character can be added to t.

Ex: Given the following strings...

s = "foobar", t = "barfoot", return 't'
s = "ide", t = "idea", return 'a'
s = "coding", t "ingcod", return ''
fun findTheDifference(s: String, t: String): Char {
    var res = 0
    s.forEach {
        res = res xor it.code
    }
    
    t.forEach {
        res = res xor it.code
    }
    
    return res.toChar()
}

Enjoying these posts? Subscribe for more