java.lang.Object
g1701_1800.s1754_largest_merge_of_two_strings.Solution

public class Solution extends Object
1754 - Largest Merge Of Two Strings.<p>Medium</p> <p>You are given two strings <code>word1</code> and <code>word2</code>. You want to construct a string <code>merge</code> in the following way: while either <code>word1</code> or <code>word2</code> are non-empty, choose <strong>one</strong> of the following options:</p> <ul> <li>If <code>word1</code> is non-empty, append the <strong>first</strong> character in <code>word1</code> to <code>merge</code> and delete it from <code>word1</code>. <ul> <li>For example, if <code>word1 = &quot;abc&quot;</code> and <code>merge = &quot;dv&quot;</code>, then after choosing this operation, <code>word1 = &quot;bc&quot;</code> and <code>merge = &quot;dva&quot;</code>.</li> </ul> </li> <li>If <code>word2</code> is non-empty, append the <strong>first</strong> character in <code>word2</code> to <code>merge</code> and delete it from <code>word2</code>. <ul> <li>For example, if <code>word2 = &quot;abc&quot;</code> and <code>merge = &quot;&quot;</code>, then after choosing this operation, <code>word2 = &quot;bc&quot;</code> and <code>merge = &quot;a&quot;</code>.</li> </ul> </li> </ul> <p>Return <em>the lexicographically <strong>largest</strong></em> <code>merge</code> <em>you can construct</em>.</p> <p>A string <code>a</code> is lexicographically larger than a string <code>b</code> (of the same length) if in the first position where <code>a</code> and <code>b</code> differ, <code>a</code> has a character strictly larger than the corresponding character in <code>b</code>. For example, <code>&quot;abcd&quot;</code> is lexicographically larger than <code>&quot;abcc&quot;</code> because the first position they differ is at the fourth character, and <code>d</code> is greater than <code>c</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;cabaa&rdquo;, word2 = &ldquo;bcaaa&rdquo;</p> <p><strong>Output:</strong> &ldquo;cbcabaaaaa&rdquo;</p> <p><strong>Explanation:</strong> One way to get the lexicographically largest merge is:</p> <ul> <li> <p>Take from word1: merge = &ldquo;c&rdquo;, word1 = &ldquo;abaa&rdquo;, word2 = &ldquo;bcaaa&rdquo;</p> </li> <li> <p>Take from word2: merge = &ldquo;cb&rdquo;, word1 = &ldquo;abaa&rdquo;, word2 = &ldquo;caaa&rdquo;</p> </li> <li> <p>Take from word2: merge = &ldquo;cbc&rdquo;, word1 = &ldquo;abaa&rdquo;, word2 = &ldquo;aaa&rdquo;</p> </li> <li> <p>Take from word1: merge = &ldquo;cbca&rdquo;, word1 = &ldquo;baa&rdquo;, word2 = &ldquo;aaa&rdquo;</p> </li> <li> <p>Take from word1: merge = &ldquo;cbcab&rdquo;, word1 = &ldquo;aa&rdquo;, word2 = &ldquo;aaa&rdquo;</p> </li> <li> <p>Append the remaining 5 a&rsquo;s from word1 and word2 at the end of merge.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> word1 = &ldquo;abcabc&rdquo;, word2 = &ldquo;abdcaba&rdquo;</p> <p><strong>Output:</strong> &ldquo;abdcabcabcaba&rdquo;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= word1.length, word2.length <= 3000</code></li> <li><code>word1</code> and <code>word2</code> consist only of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details