LeetCode Entry

389. Find the Difference

25.09.2023 easy 2023 kotlin

Strings difference by a single char

389. Find the Difference easy blog post substack image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/350

Problem TLDR

Strings difference by a single char

Intuition

We can use frequency map. Or just calculate total sum by Char Int value.

Approach

Let’s use Kotlin’s API sumBy

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code


    fun findTheDifference(s: String, t: String) =
      (t.sumBy { it.toInt() } - s.sumBy { it.toInt() }).toChar()