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
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
โ Quadratic Growth
Space Complexity
O(n)
Recursion depth is at most O(n)
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code