Check if String Is Decomposable Into Value-Equal Substrings - Problem

A value-equal string is a string where all characters are the same. For example, "1111" and "33" are value-equal strings. In contrast, "123" is not a value-equal string.

Given a digit string s, decompose the string into some number of consecutive value-equal substrings where exactly one substring has a length of 2 and the remaining substrings have a length of 3.

Return true if you can decompose s according to the above rules. Otherwise, return false.

A substring is a contiguous sequence of characters in a string.

Input & Output

Example 1 — Valid Decomposition
$ Input: s = "00111"
Output: true
💡 Note: We can decompose "00111" into "00" (length 2) and "111" (length 3). Exactly one substring has length 2.
Example 2 — Invalid Decomposition
$ Input: s = "01111"
Output: false
💡 Note: We cannot form value-equal substrings since '0' and '1' are different. Groups must contain identical characters.
Example 3 — Multiple Groups Invalid
$ Input: s = "000111"
Output: false
💡 Note: Both "000" and "111" have length 3, so we have zero groups of length 2, but we need exactly one.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s consists only of digits

Visualization

Tap to expand
String Decomposition: Value-Equal SubstringsInput: "00111"00111Length 2 ✓Length 3 ✓Rule: Exactly one substring of length 2, others length 3Output: true
Understanding the Visualization
1
Input
String with consecutive identical characters
2
Group
Identify groups of consecutive identical characters
3
Validate
Check if exactly one group can contribute length-2 substring
Key Takeaway
🎯 Key Insight: Each group of identical characters must have length that is either divisible by 3 or leaves remainder 2, with exactly one group having remainder 2
Asked in
Google 25 Microsoft 18
12.5K Views
Medium Frequency
~15 min Avg. Time
487 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