{"id":8431,"date":"2021-05-02T23:25:54","date_gmt":"2021-05-03T06:25:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8431"},"modified":"2021-05-02T23:26:15","modified_gmt":"2021-05-03T06:26:15","slug":"leetcode-1849-splitting-a-string-into-descending-consecutive-values","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-1849-splitting-a-string-into-descending-consecutive-values\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1849. Splitting a String Into Descending Consecutive Values"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;that consists of only digits.<\/p>\n\n\n\n<p>Check if we can split&nbsp;<code>s<\/code>&nbsp;into&nbsp;<strong>two or more non-empty substrings<\/strong>&nbsp;such that the&nbsp;<strong>numerical values<\/strong>&nbsp;of the substrings are in&nbsp;<strong>descending order<\/strong>&nbsp;and the&nbsp;<strong>difference<\/strong>&nbsp;between numerical values of every two&nbsp;<strong>adjacent<\/strong>&nbsp;<strong>substrings<\/strong>&nbsp;is equal to&nbsp;<code>1<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, the string&nbsp;<code>s = \"0090089\"<\/code>&nbsp;can be split into&nbsp;<code>[\"0090\", \"089\"]<\/code>&nbsp;with numerical values&nbsp;<code>[90,89]<\/code>. The values are in descending order and adjacent values differ by&nbsp;<code>1<\/code>, so this way is valid.<\/li><li>Another example, the string&nbsp;<code>s = \"001\"<\/code>&nbsp;can be split into&nbsp;<code>[\"0\", \"01\"]<\/code>,&nbsp;<code>[\"00\", \"1\"]<\/code>, or&nbsp;<code>[\"0\", \"0\", \"1\"]<\/code>. However all the ways are invalid because they have numerical values&nbsp;<code>[0,1]<\/code>,&nbsp;<code>[0,1]<\/code>, and&nbsp;<code>[0,0,1]<\/code>&nbsp;respectively, all of which are not in descending order.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if it is possible to split<\/em>&nbsp;<code>s<\/code>\u200b\u200b\u200b\u200b\u200b\u200b&nbsp;<em>as described above<\/em><em>, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters in a string.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"1234\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> There is no valid way to split s.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"050043\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> s can be split into [\"05\", \"004\", \"3\"] with numerical values [5,4,3].\nThe values are in descending order with adjacent values differing by 1.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"9080701\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> There is no valid way to split s.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"10009998\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> s can be split into [\"100\", \"099\", \"98\"] with numerical values [100,99,98].\nThe values are in descending order with adjacent values differing by 1.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 20<\/code><\/li><li><code>s<\/code>&nbsp;only consists of digits.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(2<sup>n<\/sup>)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  bool splitString(string s) {\n    const int n = s.length();\n    vector<long> nums;\n    function<bool(int)> dfs = [&](int p) {\n      if (p == n) return nums.size() >= 2;\n      long cur = 0;\n      for (int i = p; i < n &#038;&#038; cur < 1e11; ++i) {        \n        cur = cur * 10 + (s[i] - '0');\n        if (nums.empty() || cur + 1 == nums.back()) {\n          nums.push_back(cur);\n          if (dfs(i + 1)) return true;\n          nums.pop_back();\n        }\n        if (!nums.empty() &#038;&#038; cur >= nums.back()) break;                \n      }\n      return false;\n    };\n    return dfs(0);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;that consists of only digits. Check if we can split&nbsp;s&nbsp;into&nbsp;two or more non-empty substrings&nbsp;such that the&nbsp;numerical values&nbsp;of the substrings are in&nbsp;descending&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,177,42],"class_list":["post-8431","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8431"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8431\/revisions"}],"predecessor-version":[{"id":8433,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8431\/revisions\/8433"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}