14.08.2026
3090. Maximum Length Substring With Two Occurrences easy
https://dmitrysamoylenko.com/leetcode/
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 _
}

