{"id":8088,"date":"2021-02-07T17:22:26","date_gmt":"2021-02-08T01:22:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8088"},"modified":"2021-02-07T18:11:50","modified_gmt":"2021-02-08T02:11:50","slug":"leetcode-1754-largest-merge-of-two-strings","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1754-largest-merge-of-two-strings\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1754. Largest Merge Of Two Strings"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>. You want to construct a string&nbsp;<code>merge<\/code>&nbsp;in the following way: while either&nbsp;<code>word1<\/code>&nbsp;or&nbsp;<code>word2<\/code>&nbsp;are non-empty, choose&nbsp;<strong>one<\/strong>&nbsp;of the following options:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If&nbsp;<code>word1<\/code>&nbsp;is non-empty, append the&nbsp;<strong>first<\/strong>&nbsp;character in&nbsp;<code>word1<\/code>&nbsp;to&nbsp;<code>merge<\/code>&nbsp;and delete it from&nbsp;<code>word1<\/code>.<ul><li>For example, if&nbsp;<code>word1 = \"abc\"&nbsp;<\/code>and&nbsp;<code>merge = \"dv\"<\/code>, then after choosing this operation,&nbsp;<code>word1 = \"bc\"<\/code>&nbsp;and&nbsp;<code>merge = \"dva\"<\/code>.<\/li><\/ul><\/li><li>If&nbsp;<code>word2<\/code>&nbsp;is non-empty, append the&nbsp;<strong>first<\/strong>&nbsp;character in&nbsp;<code>word2<\/code>&nbsp;to&nbsp;<code>merge<\/code>&nbsp;and delete it from&nbsp;<code>word2<\/code>.<ul><li>For example, if&nbsp;<code>word2 = \"abc\"&nbsp;<\/code>and&nbsp;<code>merge = \"\"<\/code>, then after choosing this operation,&nbsp;<code>word2 = \"bc\"<\/code>&nbsp;and&nbsp;<code>merge = \"a\"<\/code>.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the lexicographically&nbsp;<strong>largest<\/strong>&nbsp;<\/em><code>merge<\/code><em>&nbsp;you can construct<\/em>.<\/p>\n\n\n\n<p>A string&nbsp;<code>a<\/code>&nbsp;is lexicographically larger than a string&nbsp;<code>b<\/code>&nbsp;(of the same length) if in the first position where&nbsp;<code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;differ,&nbsp;<code>a<\/code>&nbsp;has a character strictly larger than the corresponding character in&nbsp;<code>b<\/code>. For example,&nbsp;<code>\"abcd\"<\/code>&nbsp;is lexicographically larger than&nbsp;<code>\"abcc\"<\/code>&nbsp;because the first position they differ is at the fourth character, and&nbsp;<code>d<\/code>&nbsp;is greater than&nbsp;<code>c<\/code>.<\/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 = \"cabaa\", word2 = \"bcaaa\"\n<strong>Output:<\/strong> \"cbcabaaaaa\"\n<strong>Explanation:<\/strong> One way to get the lexicographically largest merge is:\n- Take from word1: merge = \"c\", word1 = \"abaa\", word2 = \"bcaaa\"\n- Take from word2: merge = \"cb\", word1 = \"abaa\", word2 = \"caaa\"\n- Take from word2: merge = \"cbc\", word1 = \"abaa\", word2 = \"aaa\"\n- Take from word1: merge = \"cbca\", word1 = \"baa\", word2 = \"aaa\"\n- Take from word1: merge = \"cbcab\", word1 = \"aa\", word2 = \"aaa\"\n- Append the remaining 5 a's from word1 and word2 at the end of merge.\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 = \"abcabc\", word2 = \"abdcaba\"\n<strong>Output:<\/strong> \"abdcabcabcaba\"\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;= 3000<\/code><\/li><li><code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>&nbsp;consist only of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Always take a single char from the largest word. (NOT just the current char).<\/p>\n\n\n\n<p>E.g. <br>ans = &#8220;&#8221;, w1 = &#8220;cabba&#8221;, w2 = &#8220;bcaaa&#8221;<br>w1 > w2, take from w1<br>ans = &#8220;c&#8221;, w1 = &#8220;abba&#8221;, w2 = &#8220;bcaaa&#8221;<br>w1 &lt; w2, take from w2<br>ans = &#8220;cb&#8221;, w1 = &#8220;abba&#8221;, w2 = &#8220;caaa&#8221;<br>w1 &lt; w2, take from w2<br>ans = &#8220;cbc&#8221;, w1 = &#8220;abba&#8221;, w2 = &#8220;aaa&#8221;<br>w1 > w2, take from w1. <span class=\"has-inline-color has-vivid-red-color\"><strong>Note: both start with &#8220;a&#8221;, but we need to compare the entire word.<\/strong><\/span><br>ans = &#8220;cbca&#8221;, w1 = &#8220;bba&#8221;, w2 = &#8220;aaa&#8221;<br>w1 > w2, take from w1<br>ans = &#8220;cbcab&#8221;, w1 = &#8220;ba&#8221;, w2 = &#8220;aaa&#8221;<br>&#8230;<\/p>\n\n\n\n<p>Time complexity: O(min(m,n)^2)<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 largestMerge(string_view w1, string_view w2) {\n    string ans;    \n    int m = w1.length(), n = w2.length();\n    int i = 0, j = 0;\n    while (i < m &#038;&#038; j < n)      \n      ans += w1.substr(i) > w2.substr(j) ? w1[i++] : w2[j++];\n    ans.append(w1.substr(i));\n    ans.append(w2.substr(j));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;word1&nbsp;and&nbsp;word2. You want to construct a string&nbsp;merge&nbsp;in the following way: while either&nbsp;word1&nbsp;or&nbsp;word2&nbsp;are non-empty, choose&nbsp;one&nbsp;of the following options: If&nbsp;word1&nbsp;is non-empty, append the&nbsp;first&nbsp;character&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,4],"class_list":["post-8088","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8088","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=8088"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8088\/revisions"}],"predecessor-version":[{"id":8093,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8088\/revisions\/8093"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}