LeetCode Entry

1624. Largest Substring Between Two Equal Characters

31.12.2023 easy 2023 kotlin

Max distance between same chars in string.

1624. Largest Substring Between Two Equal Characters easy blog post substack youtube image.png https://youtu.be/BF4M70PncfE

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/456

Problem TLDR

Max distance between same chars in string.

Intuition

We must remember the first occurrence position of each kind of character.

Complexity

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

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

Code


  fun maxLengthBetweenEqualCharacters(s: String) =
    with(mutableMapOf<Char, Int>()) {
      s.indices.maxOf { it - 1 - getOrPut(s[it]) { it } }
    }