{"id":108,"date":"2023-08-12T14:22:31","date_gmt":"2023-08-12T14:22:31","guid":{"rendered":"https:\/\/danielpgleason.com\/?p=108"},"modified":"2023-08-12T14:22:31","modified_gmt":"2023-08-12T14:22:31","slug":"leetcode-1768-merge-strings-alternatively","status":"publish","type":"post","link":"https:\/\/danielpgleason.com\/index.php\/2023\/08\/12\/leetcode-1768-merge-strings-alternatively\/","title":{"rendered":"Leetcode 1768 &#8211; Merge Strings Alternatively"},"content":{"rendered":"\n<p>You are given two strings <code>word1<\/code> and <code>word2<\/code>. Merge the strings by adding letters in alternating order, starting with <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 <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\"><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\"><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\"><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> and <code>word2<\/code> consist of lowercase English letters.<\/li><\/ul>\n\n\n\n<p>This was my solution:<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>class Solution:\n    def mergeAlternately(self, word1: str, word2: str) -&gt; str:\n        letters = []\n        for index, letter in enumerate(word1):\n            letters.append(letter)\n            try:\n                letters.append(word2[index])\n            except IndexError:\n                pass\n        \n        word1_size = len(word1)\n        if len(word2) &gt; word1_size:\n            l = list(word2[word1_size:])\n            letters.extend(l)\n\n        return &#39;&#39;.join(letters)<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string. Return the merged string. Example 1: Input: word1 = &#8220;abc&#8221;, word2 = &#8220;pqr&#8221; Output: &#8220;apbqcr&#8221; Explanation:&nbsp;The &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/danielpgleason.com\/index.php\/2023\/08\/12\/leetcode-1768-merge-strings-alternatively\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Leetcode 1768 &#8211; Merge Strings Alternatively&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts\/108"}],"collection":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":1,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/posts\/108\/revisions\/111"}],"wp:attachment":[{"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/danielpgleason.com\/index.php\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}