Find Longest Special Substring That Occurs Thrice I - Problem

You are given a string s consisting of lowercase English letters. Your task is to find the longest special substring that appears at least three times in the string.

A special substring is one that contains only a single type of character. For example:

  • "aaa", "bb", and "c" are special substrings
  • "abc", "aab", and "xy" are not special substrings

Goal: Return the length of the longest special substring that occurs at least 3 times, or -1 if no such substring exists.

Note: A substring is a contiguous sequence of characters within the string.

Input & Output

example_1.py — Basic Case
$ Input: s = "aaaa"
› Output: 2
šŸ’” Note: The special substrings are "a" (appears 4 times), "aa" (appears 3 times), "aaa" (appears 2 times), and "aaaa" (appears 1 time). The longest special substring that occurs at least thrice is "aa" with length 2.
example_2.py — Mixed Characters
$ Input: s = "abcdef"
› Output: -1
šŸ’” Note: All characters appear only once. Each single character special substring ("a", "b", "c", "d", "e", "f") appears exactly 1 time, which is less than 3. No special substring occurs at least thrice.
example_3.py — Multiple Groups
$ Input: s = "abcaba"
› Output: 1
šŸ’” Note: The character 'a' appears 3 times and 'b' appears 2 times. The special substring "a" occurs 3 times (at positions 0, 3, 5), which satisfies the requirement. The longest special substring that occurs at least thrice is "a" with length 1.

Visualization

Tap to expand
🧬 DNA Pattern Recognition AnalogyDNA Sequence: A-A-A-T-T-G-C-C-CAAATTGCCCAAA Group (3)TT Group (2)G Single (1)CCC Group (3)Pattern Analysis:AAA (length=3): A(3Ɨ), AA(2Ɨ), AAA(1Ɨ)TT (length=2): T(2Ɨ), TT(1Ɨ)G (length=1): G(1Ɨ)CCC (length=3): C(3Ɨ), CC(2Ɨ), CCC(1Ɨ)Patterns appearing ≄3 times: A(3Ɨ), C(3Ɨ) → Max length = 1šŸŽÆ Key Insight:Group consecutive identical nucleotides and use math to count patterns,avoiding the need to examine every possible subsequence individually!
Understanding the Visualization
1
Scan DNA Strand
Examine the DNA sequence character by character
2
Group Identical Nucleotides
Identify consecutive sequences of the same nucleotide (A, T, G, C)
3
Count Pattern Occurrences
For each group, calculate how many patterns of each length it contributes
4
Find Most Common Pattern
Identify the longest pattern that appears at least 3 times across the strand
Key Takeaway
šŸŽÆ Key Insight: Instead of checking every possible substring (O(n³)), group consecutive identical characters and use mathematical formulas to count special substrings efficiently in O(n) time.

Time & Space Complexity

Time Complexity
ā±ļø
O(n)

Single pass to group characters, constant work per group

n
2n
āœ“ Linear Growth
Space Complexity
O(n)

Hash map to store counts for each (character, length) combination

n
2n
⚔ Linearithmic Space

Constraints

  • 3 ≤ s.length ≤ 50
  • s consists of only lowercase English letters
  • A special substring must occur at least 3 times
  • Return -1 if no valid special substring exists
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24
32.4K Views
Medium-High Frequency
~18 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen