LeetCode Entry
1624. Largest Substring Between Two Equal Characters
Max distance between same chars in string.
1624. Largest Substring Between Two Equal Characters easy
blog post
substack
youtube
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 } }
}