1849. Splitting a String Into Descending Consecutive Values

Difficulty:
Related Topics:
Similar Questions:

    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.

    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:

    Solution (Java)

    class Solution {
        public boolean splitString(String s) {
            return solve(0, -1, s, 0);
        }
    
        private boolean solve(int i, long prev, String s, int k) {
            if (i == s.length()) {
                return k >= 2;
            }
            long cur = 0;
            for (int j = i; j < s.length(); j++) {
                cur = cur * 10 + s.charAt(j) - '0';
                if ((prev == -1 || prev - cur == 1) && solve(j + 1, cur, s, k + 1)) {
                    return true;
                }
            }
            return false;
        }
    }
    

    Explain:

    nope.

    Complexity: