Find Longest Special Substring That Occurs Thrice I — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

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.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Find Longest Special Substring That Occurs Thrice I
Detailed explanation and reference materials
Problem Overview

Find Longest Special Substring That Occurs Thrice

Problem Description

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.


Approach

1. Binary Search

The problem exhibits a monotonic property:

  • If a special substring of length x exists at least three times, then all substrings of length < x are also valid.
  • Conversely, if x is invalid, all x > x are also invalid.

Using binary search, we efficiently search for the largest possible x:

  • Start with l = 1 (minimum substring length) and r = n (length of the string s).
  • Use a helper function to validate if a given x is possible.

2. Sliding Window Validation

To validate if a given x is valid:

  • Use a sliding window of size x to traverse the string.
  • Ensure each window contains only identical characters.
  • Track occurrences of these substrings using a hashmap.
  • Return true if any substring occurs at least three times.

3. Iterative Binary Search

  • Perform binary search to find the largest valid x.
  • If l is valid after the search, return l.
  • Otherwise, return -1.

Complexity

  • Time Complexity:

    • Binary Search: O(log n) iterations for a string of length n.
    • Validation for each x: O(n) using sliding window.
    • Total: O(n log n).
  • Space Complexity:

    • O(n) for the hashmap used during validation.

Test Cases

Example 1

Input:

s = "aaabbbaaa"

Output:

3

Explanation:

  • The substring "aaa" occurs 3 times in the string.

Example 2

Input:

s = "abc"

Output:

-1

Explanation:

  • No special substring occurs at least three times.

Example 3

Input:

s = "aaaaaa"

Output:

6

Explanation:

  • The substring "aaaaaa" is the entire string and appears 3 times (as a whole).

Example 4

Input:

s = "abbbbbbbc"

Output:

5

Explanation:

  • The substring "bbbbb" appears at least 3 times in the string.

Example 5

Input:

s = "aabbcc"

Output:

-1

Explanation:

  • No substring satisfies the condition of occurring 3 times.

Edge Cases

  1. Single Character String: Input:

    s = "a"
    

    Output:

    -1
    

    Explanation: A single character cannot occur 3 times as a substring.

  2. String with No Repeats: Input:

    s = "abcdef"
    

    Output:

    -1
    

    Explanation: No character repeats consecutively.

  3. Already Valid Entire String: Input:

    s = "ccc"
    

    Output:

    3
    

    Explanation: The entire string forms a valid substring of length 3, occurring exactly 3 times.


— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
searching-&-sorting
java
Reference Link
https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i/description/?envType=daily-question&envId=2024-12-10

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

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 Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

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.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.