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

๐Ÿงฉ Check if String Is Decomposable Into Value-Equal Substrings

Imagine you're a pattern recognition expert tasked with breaking down a digit string into specific chunks. You need to determine if a given digit string can be decomposed into consecutive value-equal substrings following very specific rules.

๐Ÿ“‹ What are Value-Equal Strings?

A value-equal string is a substring where all characters are identical:

  • โœ… "1111" - all 1's
  • โœ… "33" - all 3's
  • โŒ "123" - mixed characters

๐ŸŽฏ The Challenge

Given a digit string s, you must check if it can be decomposed into consecutive value-equal substrings where:

  1. Exactly one substring has length 2
  2. All remaining substrings have length 3

Example: "00011" โ†’ ["000", "11"] โœ… (one length-3, one length-2)

Example: "111222" โ†’ Cannot be decomposed โŒ (would need two length-3 substrings)

Return true if such decomposition is possible, false otherwise.

Input & Output

example_1.py โ€” Basic Valid Case
$ Input: s = "00011"
โ€บ Output: true
๐Ÿ’ก Note: Can be decomposed as ["000", "11"] where "000" has length 3 and "11" has length 2. Exactly one substring has length 2.
example_2.py โ€” Invalid Case
$ Input: s = "111222"
โ€บ Output: false
๐Ÿ’ก Note: Groups are "111" (length 3) and "222" (length 3). Both can only be split into length-3 substrings, so we get zero length-2 substrings instead of exactly one.
example_3.py โ€” Impossible Split
$ Input: s = "1"
โ€บ Output: false
๐Ÿ’ก Note: Single character cannot be split into substrings of length 2 or 3. Group length 1 has remainder 1 when divided by 3, making it impossible to decompose.

Constraints

  • 1 โ‰ค s.length โ‰ค 105
  • s consists of digits 0-9 only
  • Important: String must be decomposable into consecutive value-equal substrings

Visualization

Tap to expand
String Decomposition VisualizationInput: "00011" โ†’ Groups: [000] [11]00011Group 1: Length = 33 % 3 = 0 (remainder 0)Group 2: Length = 22 % 3 = 2 (remainder 2)Can split into [3]Must split into [2]โœ“ VALID DECOMPOSITIONExactly 1 group with remainder 2
Understanding the Visualization
1
Identify Consecutive Groups
Scan through the string to find runs of identical characters, like grouping same-colored blocks together
2
Apply Modular Math
For each group, check if length % 3 gives remainder 0 (divisible by 3) or remainder 2 (leaves one pair)
3
Validate Constraints
Ensure exactly one group has remainder 2 (provides the length-2 substring) and no group has remainder 1 (impossible)
4
Return Result
True if exactly one remainder-2 group exists, false otherwise
Key Takeaway
๐ŸŽฏ Key Insight: The mathematical property that groups of length โ‰ก 2 (mod 3) provide exactly one length-2 substring plus some length-3 substrings, while groups of length โ‰ก 0 (mod 3) provide only length-3 substrings, allows us to solve this in O(n) time by simply counting remainder-2 groups.
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
24.5K Views
Medium Frequency
~15 min Avg. Time
890 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