Splitting a String Into Descending Consecutive Values - Problem

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.

Examples:

  • The string 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 is valid.
  • The string s = "001" can be split into ["0", "01"], ["00", "1"], or ["0", "0", "1"]. However, all 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.

Input & Output

Example 1 — Valid Split with Leading Zeros
$ Input: s = "0090089"
Output: true
💡 Note: Can split into ["0090", "089"] with numerical values [90, 89]. Values are descending (90 > 89) and differ by 1 (90 - 89 = 1).
Example 2 — No Valid Split
$ Input: s = "001"
Output: false
💡 Note: Possible splits: ["0","01"], ["00","1"], ["0","0","1"] give values [0,1], [0,1], [0,0,1]. None are descending consecutive.
Example 3 — Simple Descending
$ Input: s = "4321"
Output: true
💡 Note: Can split into ["4","3","2","1"] with values [4,3,2,1]. Values are descending and each differs by exactly 1: 4-3=1, 3-2=1, 2-1=1.

Constraints

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

Visualization

Tap to expand
Splitting String Into Descending Consecutive Values INPUT String s = "0090089" 0 0 9 0 0 8 9 0 1 2 3 4 5 6 Valid Split Found: "0090" "089" = 90 = 89 90 - 89 = 1 (OK) Requirements: - Split into 2+ substrings - Descending order - Difference = 1 between adjacent ALGORITHM STEPS 1 Try First Split Pick first number length 2 Handle Leading Zeros Skip invalid: "00" not 0 3 Backtrack Search Find next = current - 1 4 Validate Chain Must use entire string Backtracking Tree: "0090089" "0" X "0090" "089" 90 --> 89 OK FINAL RESULT true Valid split exists! Solution Breakdown: "0090" = 90 "089" = 89 90 - 89 = 1 (consecutive) Descending: 90 > 89 (OK) Leading Zeros Handled: "0090" has leading zero but value 90 is valid "089" = 89 (also valid) Only "0" can have leading 0 Key Insight: Optimized backtracking prunes invalid paths early by checking leading zeros. A substring with leading zeros is only valid if it equals "0". Once we find a valid first number, we search for (num-1) in the remaining string. Time complexity: O(n^2) where n is string length. Each position checked once per path. TutorialsPoint - Splitting a String Into Descending Consecutive Values | Optimized Backtracking
Asked in
Google 23 Facebook 18 Amazon 15
28.4K 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