LeetCode Entry

3090. Maximum Length Substring With Two Occurrences

14.08.2026 easy 2026 kotlin rust

Max substring repeats less than 3

3090. Maximum Length Substring With Two Occurrences easy substack youtube

https://dmitrysamoylenko.com/leetcode/

14.08.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1451

Problem TLDR

Max substring repeats less than 3

Intuition

Brute force. Try every length from largest to smalest.

Approach

  • Rust: rfind
  • Kotlin: find

Complexity

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

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

Code

    fun maximumLengthSubstring(s: String) = (s.length downTo 2)
    .find {s.windowed(it).any{w->w.all{w.count{c->c==it}<3}}}
    pub fn maximum_length_substring(s: String) -> i32 {
        (1..=s.len()).rfind(|&n|s.as_bytes().windows(n)
        .any(|w|w.iter().counts().values().all(|&v|v<3))).unwrap() as _
    }

Comments