How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
You are given a string s, and your task is to find the maximum length x of a special substring that appears at least three times in s.
A special substring is defined as a substring containing only one unique character (e.g., "aaa", "bbbb").
If no such substring exists, return -1.
The problem exhibits a monotonic property:
x exists at least three times, then all substrings of length < x are also valid.x is invalid, all x > x are also invalid.Using binary search, we efficiently search for the largest possible x:
l = 1 (minimum substring length) and r = n (length of the string s).x is possible.To validate if a given x is valid:
x to traverse the string.true if any substring occurs at least three times.x.l is valid after the search, return l.-1.Time Complexity:
O(log n) iterations for a string of length n.x: O(n) using sliding window.O(n log n).Space Complexity:
O(n) for the hashmap used during validation.Input:
s = "aaabbbaaa"
Output:
3
Explanation:
"aaa" occurs 3 times in the string.Input:
s = "abc"
Output:
-1
Explanation:
Input:
s = "aaaaaa"
Output:
6
Explanation:
"aaaaaa" is the entire string and appears 3 times (as a whole).Input:
s = "abbbbbbbc"
Output:
5
Explanation:
"bbbbb" appears at least 3 times in the string.Input:
s = "aabbcc"
Output:
-1
Explanation:
Single Character String: Input:
s = "a"
Output:
-1
Explanation: A single character cannot occur 3 times as a substring.
String with No Repeats: Input:
s = "abcdef"
Output:
-1
Explanation: No character repeats consecutively.
Already Valid Entire String: Input:
s = "ccc"
Output:
3
Explanation: The entire string forms a valid substring of length 3, occurring exactly 3 times.
🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
🔒 Locked steps cannot be moved. Only unlocked steps are draggable.
🔊 Enable sound for swap feedback and completion effects.