Splitting a String Into Descending Consecutive Values - Problem

You're given a string s consisting of only digits. Your task is to determine if you can split this string into two or more non-empty substrings such that:

  • The numerical values of the substrings form a descending sequence
  • Each adjacent pair differs by exactly 1

Example: The string "0090089" can be split into ["0090", "089"] with values [90, 89] - a valid descending sequence where each number is 1 less than the previous.

Note: Leading zeros are allowed in substrings, but they don't affect the numerical value (e.g., "089" = 89).

Input & Output

example_1.py โ€” Basic Valid Split
$ Input: s = "1234321"
โ€บ Output: false
๐Ÿ’ก Note: No way to split this into a descending sequence where adjacent numbers differ by 1. Even trying [1234, 321] gives us numbers that don't differ by 1.
example_2.py โ€” Valid with Leading Zeros
$ Input: s = "0090089"
โ€บ Output: true
๐Ÿ’ก Note: Can be split as ["0090", "089"] which gives numerical values [90, 89]. These form a descending sequence with difference of 1.
example_3.py โ€” Simple Descending
$ Input: s = "10009998"
โ€บ Output: true
๐Ÿ’ก Note: Can be split as ["1000", "999", "998"] giving values [1000, 999, 998] - a perfect descending sequence with differences of 1.

Visualization

Tap to expand
๐Ÿ• Digital Countdown Parser1 0 0 9 9 8 9 7Broken Display - Separators Missing!Try Split 1:[1] [009] [9] [8] [9] [7]โŒ Not consecutiveTry Split 2:[100] [99] [8] [97]โŒ 99โ†’8 not -1Try Split 3:[1009] [998] [97]โŒ 998โ†’97 not -1โœ… Split 4:[100] [99] [98] [97]โœ“ Perfect countdown!๐Ÿ” Algorithm InsightOnce we choose the first number (100),the entire countdown sequence is determined!We just verify: 99, 98, 97...๐Ÿ’ก Key: Try each possible first number length, then validate the pattern
Understanding the Visualization
1
Choose Starting Number
Pick how many digits form the first number (like deciding where the first separator goes)
2
Determine Sequence
Once first number is chosen, we know exactly what the rest should be (countdown by 1)
3
Pattern Matching
Check if remaining digits match the expected countdown pattern
4
Handle Variations
Account for leading zeros in the display representation
Key Takeaway
๐ŸŽฏ Key Insight: This problem transforms from exponential search space to polynomial time by recognizing that fixing the first number completely determines the expected sequence, allowing us to validate deterministically rather than exploring all possibilities.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each starting position, we make at most O(n) recursive calls

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion depth is at most O(n)

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 20
  • s consists of only digits
  • Each substring must be non-empty
  • Need at least 2 substrings for valid split
  • Leading zeros are allowed in substrings
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
24.5K Views
Medium Frequency
~25 min Avg. Time
892 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