{"id":8143,"date":"2021-02-20T22:46:17","date_gmt":"2021-02-21T06:46:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8143"},"modified":"2021-02-20T22:47:25","modified_gmt":"2021-02-21T06:47:25","slug":"leetcode-1768-merge-strings-alternately","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-1768-merge-strings-alternately\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1768. Merge Strings Alternately"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>. Merge the strings by adding letters in alternating order, starting with&nbsp;<code>word1<\/code>. If a string is longer than the other, append the additional letters onto the end of the merged string.<\/p>\n\n\n\n<p>Return&nbsp;<em>the merged string.<\/em><\/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 = \"abc\", word2 = \"pqr\"\n<strong>Output:<\/strong> \"apbqcr\"\n<strong>Explanation:<\/strong>&nbsp;The merged string will be merged as so:\nword1:  a   b   c\nword2:    p   q   r\nmerged: a p b q c r\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> word1 = \"ab\", word2 = \"pqrs\"\n<strong>Output:<\/strong> \"apbqrs\"\n<strong>Explanation:<\/strong>&nbsp;Notice that as word2 is longer, \"rs\" is appended to the end.\nword1:  a   b \nword2:    p   q   r   s\nmerged: a p b q   r   s\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 = \"abcd\", word2 = \"pq\"\n<strong>Output:<\/strong> \"apbqcd\"\n<strong>Explanation:<\/strong>&nbsp;Notice that as word1 is longer, \"cd\" is appended to the end.\nword1:  a   b   c   d\nword2:    p   q \nmerged: a p b q c   d\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;= 100<\/code><\/li><li><code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>&nbsp;consist of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Find the shorter one<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(m+n)<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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string mergeAlternately(string word1, string word2) {\n    const int l1 = word1.size(), l2 = word2.size();\n    string ans;\n    for (int i = 0; i < min(l1, l2); ++i) {\n      ans += word1[i]; \n      ans += word2[i];\n    }\n    ans += l1 > l2 ? word1.substr(l2) : word2.substr(l1);    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;word1&nbsp;and&nbsp;word2. Merge the strings by adding letters in alternating order, starting with&nbsp;word1. If a string is longer than the other, append&#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-8143","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\/8143","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=8143"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8143\/revisions"}],"predecessor-version":[{"id":8145,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8143\/revisions\/8145"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}