Watch 8 video solutions for Splitting a String Into Descending Consecutive Values, a medium level problem involving String, Backtracking. This walkthrough by NeetCode has 21,667 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given a string s that consists of only digits.
Check if we can split s into two or more non-empty substrings such that the numerical values of the substrings are in descending order and the difference between numerical values of every two adjacent substrings is equal to 1.
s = "0090089" can be split into ["0090", "089"] with numerical values [90,89]. The values are in descending order and adjacent values differ by 1, so this way is valid.s = "001" can be split into ["0", "01"], ["00", "1"], or ["0", "0", "1"]. However all the ways are invalid because they have numerical values [0,1], [0,1], and [0,0,1] respectively, all of which are not in descending order.Return true if it is possible to split s as described above, or false otherwise.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "1234" Output: false Explanation: There is no valid way to split s.
Example 2:
Input: s = "050043" Output: true Explanation: s can be split into ["05", "004", "3"] with numerical values [5,4,3]. The values are in descending order with adjacent values differing by 1.
Example 3:
Input: s = "9080701" Output: false Explanation: There is no valid way to split s.
Constraints:
1 <= s.length <= 20s only consists of digits.Problem Overview: You receive a numeric string and must determine whether it can be split into two or more numbers such that every number is exactly 1 less than the previous one. The values must appear in strictly descending consecutive order and each part must preserve its original digit sequence.
Approach 1: Recursive Backtracking (Time: O(2^n), Space: O(n))
This approach tries every possible split for the first number, then recursively checks whether the remaining substring can form a chain of values decreasing by exactly one. After choosing the first number, each recursive step builds the next number digit by digit and compares it with previous - 1. If the value becomes larger than expected, the branch stops early, which prunes many invalid paths. The recursion continues until the entire string is consumed and at least two numbers were formed. This method naturally fits backtracking problems because you explore candidate partitions and abandon invalid states immediately. The implementation usually converts substrings into integers (or uses incremental numeric construction) and keeps track of the last chosen value.
Approach 2: Iterative Reduction with Queue (Time: O(n^2), Space: O(n))
This strategy simulates the splitting process iteratively. Start by generating possible prefixes for the first number and push them into a queue along with the index where the next number should begin. For each queued state, build the next number character by character while comparing it to previous - 1. If the numbers match the required descending pattern, push the new state back into the queue and continue scanning the string. The queue acts as a frontier of partial sequences still being validated. This avoids deep recursion and can be easier to reason about in iterative environments such as JavaScript or C#. The approach still relies heavily on string scanning and numeric comparison, which is common in string parsing tasks.
Both strategies rely on the same key insight: once the first number is fixed, the rest of the sequence is fully determined because each next value must equal previous - 1. The algorithm only needs to verify whether the remaining digits can construct those exact numbers without skipping digits.
Recommended for interviews: Recursive backtracking is the approach most interviewers expect. It clearly demonstrates control over recursion, pruning, and sequence validation using recursion. The queue-based method is a solid alternative when recursion depth is a concern, but the recursive version communicates the core idea more directly.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Recursive Backtracking | O(2^n) | O(n) | Best for interviews and typical solutions; clean exploration of all valid splits with pruning |
| Iterative Reduction with Queue | O(n^2) | O(n) | Useful when avoiding recursion or implementing state exploration iteratively |