LeetCode Entry

1347. Minimum Number of Steps to Make Two Strings Anagram

13.01.2024 medium 2024 kotlin

Min operations to make string t anagram of s.

1347. Minimum Number of Steps to Make Two Strings Anagram medium blog post substack youtube image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/470

Problem TLDR

Min operations to make string t anagram of s.

Intuition

Let’s compare char’s frequencies of those two strings.

Approach

  • careful: as we replacing one kind of chars with another, we must decrease that another counter

Complexity

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

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

Code


  fun minSteps(s: String, t: String) =
    IntArray(128).let {
      for (c in s) it[c.toInt()]++
      for (c in t) it[c.toInt()]--
      it.sumOf { abs(it) } / 2
    }