{"id":7696,"date":"2020-11-22T00:30:11","date_gmt":"2020-11-22T08:30:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7696"},"modified":"2020-11-22T00:30:29","modified_gmt":"2020-11-22T08:30:29","slug":"leetcode-1662-check-if-two-string-arrays-are-equivalent","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1662-check-if-two-string-arrays-are-equivalent\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1662. Check If Two String Arrays are Equivalent"},"content":{"rendered":"\n<p>Given two string arrays&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>, return<code>true<\/code><em>&nbsp;if the two arrays&nbsp;<strong>represent<\/strong>&nbsp;the same string, and&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/p>\n\n\n\n<p>A string is&nbsp;<strong>represented<\/strong>&nbsp;by an array if the array elements concatenated&nbsp;<strong>in order<\/strong>&nbsp;forms the 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> word1 = [\"ab\", \"c\"], word2 = [\"a\", \"bc\"]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>\nword1 represents string \"ab\" + \"c\" -&gt; \"abc\"\nword2 represents string \"a\" + \"bc\" -&gt; \"abc\"\nThe strings are the same, so return true.<\/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> word1 = [\"a\", \"cb\"], word2 = [\"ab\", \"c\"]\n<strong>Output:<\/strong> false\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> word1  = [\"abc\", \"d\", \"defg\"], word2 = [\"abcddefg\"]\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>1 &lt;= word1[i].length, word2[i].length &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>1 &lt;= sum(word1[i].length), sum(word2[i].length) &lt;= 10<sup>3<\/sup><\/code><\/li><li><code>word1[i]<\/code>&nbsp;and&nbsp;<code>word2[i]<\/code>&nbsp;consist of lowercase letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1: Construct the string<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(l1 + l2)<br>Space complexity: O(l1 + l2)<\/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 arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {\n    string s1, s2;\n    for (const string& w1 : word1) s1 += w1;\n    for (const string& w2 : word2) s2 += w2;\n    return s1 == s2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Pointers<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(l1 + l2)<br>Space complexity: O(1)<\/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 arrayStringsAreEqual(vector<string>& word1, vector<string>& word2) {\n    int i1 = 0, j1 = 0;\n    int i2 = 0, j2 = 0;\n    while (i1 < word1.size() || i2 < word2.size()) {\n      char c1 = i1 < word1.size() ? word1[i1][j1++] : '\\0';\n      char c2 = i2 < word2.size() ? word2[i2][j2++] : '\\0';\n      if (c1 != c2) return false;\n      if (i1 < word1.size() &#038;&#038; j1 == word1[i1].length())\n        ++i1, j1 = 0;\n      if (i2 < word2.size() &#038;&#038; j2 == word2[i2].length())\n        ++i2, j2 = 0;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given two string arrays&nbsp;word1&nbsp;and&nbsp;word2, returntrue&nbsp;if the two arrays&nbsp;represent&nbsp;the same string, and&nbsp;false&nbsp;otherwise. A string is&nbsp;represented&nbsp;by an array if the array elements concatenated&nbsp;in order&nbsp;forms the string. Example&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[20,669,222,4],"class_list":["post-7696","post","type-post","status-publish","format-standard","hentry","category-string","tag-array","tag-concatenation","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7696","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=7696"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7696\/revisions"}],"predecessor-version":[{"id":7698,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7696\/revisions\/7698"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}