You are given a string s of digits. Determine whether s can be split into a sequence of two or more non-empty substrings such that the numerical values of the substrings form a sequential combination, meaning each number is exactly one greater than the previous number.
Leading zeros are not allowed in any substring (except for the number 0 itself, which must be the single character "0"). Return True if at least one valid split exists, otherwise return False.
Function Signature
def split_into_sequential(s: str) -> bool:
Rules
-
The pieces must cover the whole string in order, and there must be at least two of them.
-
Each piece is read as a base-10 number. A piece longer than one character may not start with
"0"
. The single piece
"0"
is allowed and means zero.
Constraints
-
1 <= s.length <= 20
-
s
consists of only digits.
Examples
Input: s = "123456"
Output: True
Splitting into "1", "2", "3", "4", "5", "6" gives numbers that each go up by one.
Input: s = "99100101"
Output: True
Splitting into "99", "100", "101" works even though the digit count grows from two to three.
Input: s = "0102"
Output: False
"01" and "02" would be consecutive, but both have a leading zero. The only allowed first piece is "0", which forces the sequence 0, 1, 2 and the string "012", not "0102".