|
8 | 8 |
|
9 | 9 | <ul>
|
10 | 10 | <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>.
|
11 |
| - |
12 | 11 | <ul>
|
13 | 12 | <li>For example, if <code>word1 = "abc" </code>and <code>merge = "dv"</code>, then after choosing this operation, <code>word1 = "bc"</code> and <code>merge = "dva"</code>.</li>
|
14 | 13 | </ul>
|
|
18 | 17 | <li>For example, if <code>word2 = "abc" </code>and <code>merge = ""</code>, then after choosing this operation, <code>word2 = "bc"</code> and <code>merge = "a"</code>.</li>
|
19 | 18 | </ul>
|
20 | 19 | </li>
|
21 |
| - |
22 | 20 | </ul>
|
23 | 21 |
|
24 | 22 | <p>Return <em>the lexicographically <strong>largest</strong> </em><code>merge</code><em> you can construct</em>.</p>
|
|
62 | 60 | ### **Python3**
|
63 | 61 |
|
64 | 62 | ```python
|
65 |
| - |
| 63 | +class Solution: |
| 64 | + def largestMerge(self, word1: str, word2: str) -> str: |
| 65 | + i = j = 0 |
| 66 | + ans = [] |
| 67 | + while i < len(word1) and j < len(word2): |
| 68 | + if word1[i:] > word2[j:]: |
| 69 | + ans.append(word1[i]) |
| 70 | + i += 1 |
| 71 | + else: |
| 72 | + ans.append(word2[j]) |
| 73 | + j += 1 |
| 74 | + ans.append(word1[i:]) |
| 75 | + ans.append(word2[j:]) |
| 76 | + return "".join(ans) |
66 | 77 | ```
|
67 | 78 |
|
68 | 79 | ### **Java**
|
69 | 80 |
|
70 | 81 | ```java
|
| 82 | +class Solution { |
| 83 | + public String largestMerge(String word1, String word2) { |
| 84 | + int m = word1.length(), n = word2.length(); |
| 85 | + int i = 0, j = 0; |
| 86 | + StringBuilder ans = new StringBuilder(); |
| 87 | + while (i < m && j < n) { |
| 88 | + boolean gt = word1.substring(i).compareTo(word2.substring(j)) > 0; |
| 89 | + ans.append(gt ? word1.charAt(i++) : word2.charAt(j++)); |
| 90 | + } |
| 91 | + ans.append(word1.substring(i)); |
| 92 | + ans.append(word2.substring(j)); |
| 93 | + return ans.toString(); |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### **C++** |
| 99 | + |
| 100 | +```cpp |
| 101 | +class Solution { |
| 102 | +public: |
| 103 | + string largestMerge(string word1, string word2) { |
| 104 | + int m = word1.size(), n = word2.size(); |
| 105 | + int i = 0, j = 0; |
| 106 | + string ans; |
| 107 | + while (i < m && j < n) { |
| 108 | + bool gt = word1.substr(i) > word2.substr(j); |
| 109 | + ans += gt ? word1[i++] : word2[j++]; |
| 110 | + } |
| 111 | + ans += word1.substr(i); |
| 112 | + ans += word2.substr(j); |
| 113 | + return ans; |
| 114 | + } |
| 115 | +}; |
| 116 | +``` |
71 | 117 |
|
| 118 | +### **Go** |
| 119 | +
|
| 120 | +```go |
| 121 | +func largestMerge(word1 string, word2 string) string { |
| 122 | + m, n := len(word1), len(word2) |
| 123 | + i, j := 0, 0 |
| 124 | + var ans strings.Builder |
| 125 | + for i < m && j < n { |
| 126 | + if word1[i:] > word2[j:] { |
| 127 | + ans.WriteByte(word1[i]) |
| 128 | + i++ |
| 129 | + } else { |
| 130 | + ans.WriteByte(word2[j]) |
| 131 | + j++ |
| 132 | + } |
| 133 | + } |
| 134 | + ans.WriteString(word1[i:]) |
| 135 | + ans.WriteString(word2[j:]) |
| 136 | + return ans.String() |
| 137 | +} |
72 | 138 | ```
|
73 | 139 |
|
74 | 140 | ### **...**
|
|
0 commit comments