LeetCode Entry

1897. Redistribute Characters to Make All Strings Equal

30.12.2023 easy 2023 kotlin

Is it possible to split all the words[] characters into words.size groups.

1897. Redistribute Characters to Make All Strings Equal easy blog post substack youtube image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/455

Problem TLDR

Is it possible to split all the words[] characters into words.size groups.

Intuition

To understand the problem, consider example: a abc abbcc -> [abc] [abc] [abc]. We know the result words count, and we know the count of each kind of character. So, just make sure, every character’s count can be separated into words.size groups.

Approach

  • to better understand the problem, consider adding more examples
  • there can be more than one repeating character in group, [aabc] [aabc] [aabc]

Complexity

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

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

Code


  fun makeEqual(words: Array<String>) =
    IntArray(26).apply {
      for (w in words) for (c in w) this[c.toInt() - 'a'.toInt()]++
    }.all { it % words.size == 0 }