#1849

Splitting a String Into Descending Consecutive Values

expert · 1060 · lc medium +32 · failed · 37.5% accepted · 551 likes · top 16%

Description

Given a string s consisting only of digits, determine whether it can be split into two or more non-empty substrings such that their numeric values form a strictly descending sequence with each consecutive pair differing by exactly 1.

- For example, "0090089" can be split into ["0090", "089"] giving numeric values [90, 89] which satisfies the condition.

Return true if such a valid split exists, or false otherwise.

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.

Code

1
2
3