{"id":4927,"date":"2019-03-02T20:33:10","date_gmt":"2019-03-03T04:33:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4927"},"modified":"2019-03-02T20:33:34","modified_gmt":"2019-03-03T04:33:34","slug":"leetcode-1003-check-if-word-is-valid-after-substitutions","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1003-check-if-word-is-valid-after-substitutions\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1003. Check If Word Is Valid After Substitutions"},"content":{"rendered":"\n<p>We are given that the string&nbsp;<code>\"abc\"<\/code>&nbsp;is valid.<\/p>\n\n\n\n<p>From any valid string&nbsp;<code>V<\/code>, we may split&nbsp;<code>V<\/code>&nbsp;into two pieces&nbsp;<code>X<\/code>&nbsp;and&nbsp;<code>Y<\/code>&nbsp;such that&nbsp;<code>X + Y<\/code>&nbsp;(<code>X<\/code>&nbsp;concatenated with&nbsp;<code>Y<\/code>) is equal to&nbsp;<code>V<\/code>.&nbsp; (<code>X<\/code>&nbsp;or&nbsp;<code>Y<\/code>&nbsp;may be empty.)&nbsp; Then,&nbsp;<code>X + \"abc\" + Y<\/code>&nbsp;is also valid.<\/p>\n\n\n\n<p>If for example&nbsp;<code>S = \"abc\"<\/code>, then examples of valid strings are:&nbsp;<code>\"abc\", \"aabcbc\", \"abcabc\", \"abcabcababcc\"<\/code>.&nbsp; Examples of&nbsp;<strong>invalid<\/strong>&nbsp;strings are:&nbsp;<code>\"abccba\"<\/code>,&nbsp;<code>\"ab\"<\/code>,&nbsp;<code>\"cababc\"<\/code>,&nbsp;<code>\"bac\"<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;if and only if the given string&nbsp;<code>S<\/code>&nbsp;is valid.<\/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>\"aabcbc\"\n<strong>Output: <\/strong>true\n<strong>Explanation: <\/strong>\nWe start with the valid string \"abc\".\nThen we can insert another \"abc\" between \"a\" and \"bc\", resulting in \"a\" + \"abc\" + \"bc\" which is \"aabcbc\".\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>\"abcabcababcc\"\n<strong>Output: <\/strong>true\n<strong>Explanation: <\/strong>\n\"abcabcabc\" is valid after consecutive insertings of \"abc\".\nThen we can insert \"abc\" before the last letter, resulting in \"abcabcab\" + \"abc\" + \"c\" which is \"abcabcababcc\".\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>\"abccba\"\n<strong>Output: <\/strong>false\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>\"cababc\"\n<strong>Output: <\/strong>false<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>1 &lt;= S.length &lt;= 20000<\/code><\/li><li><code>S[i]<\/code>&nbsp;is&nbsp;<code>'a'<\/code>,&nbsp;<code>'b'<\/code>, or&nbsp;<code>'c'<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>If current char can be appended to the stack do so, if the top of stack is &#8220;abc&#8221; pop, otherwise push the current char to the stack. Check whether the stack is empty after all chars were processed.<\/p>\n\n\n\n<p>Time complexity: O(n)<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++\">\n\/\/ Author: Huahua, running time: 28 ms, 11.9 MB\nclass Solution {\npublic:\n  bool isValid(string S) {\n    stack<string> st;\n    for (char ch : S) {\n      if (st.empty()) {\n        st.push(\"\");\n      }\n      string& s = st.top();\n      char exp = 'a' + s.length();\n      if (ch == exp) {\n        s += ch;\n        if (s == \"abc\") st.pop();\n      } else if (ch != 'a'){\n        return false;        \n      } else {\n        st.push(\"a\");\n      }\n    }\n    return st.empty();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We are given that the string&nbsp;&#8220;abc&#8221;&nbsp;is valid. From any valid string&nbsp;V, we may split&nbsp;V&nbsp;into two pieces&nbsp;X&nbsp;and&nbsp;Y&nbsp;such that&nbsp;X + Y&nbsp;(X&nbsp;concatenated with&nbsp;Y) is equal to&nbsp;V.&nbsp; (X&nbsp;or&nbsp;Y&nbsp;may be&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4927","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4927","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=4927"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4927\/revisions"}],"predecessor-version":[{"id":4929,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4927\/revisions\/4929"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}