Skip to content

Commit f129169

Browse files
committedNov 29, 2022
feat: add solutions to lc problem: No.1754
No.1754.Largest Merge Of Two Strings
1 parent 93f381b commit f129169

File tree

6 files changed

+206
-6
lines changed

6 files changed

+206
-6
lines changed
 

‎solution/1700-1799/1754.Largest Merge Of Two Strings/README.md

+77-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
<ul>
1212
<li>如果 <code>word1</code> 非空,将 <code>word1</code> 中的第一个字符附加到 <code>merge</code> 的末尾,并将其从 <code>word1</code> 中移除。
13-
1413
<ul>
1514
<li>例如,<code>word1 = "abc" </code>且 <code>merge = "dv"</code> ,在执行此选项操作之后,<code>word1 = "bc"</code> ,同时 <code>merge = "dva"</code> 。</li>
1615
</ul>
@@ -20,7 +19,6 @@
2019
<li>例如,<code>word2 = "abc" </code>且 <code>merge = ""</code> ,在执行此选项操作之后,<code>word2 = "bc"</code> ,同时 <code>merge = "a"</code> 。</li>
2120
</ul>
2221
</li>
23-
2422
</ul>
2523

2624
<p>返回你可以构造的字典序 <strong>最大</strong> 的合并字符串<em> </em><code>merge</code><em> 。</em></p>
@@ -63,22 +61,98 @@
6361

6462
<!-- 这里可写通用的实现逻辑 -->
6563

64+
**方法一:贪心 + 双指针**
65+
66+
我们用指针 $i$ 和 $j$ 分别指向字符串 `word1``word2` 的第一个字符。然后循环,每次比较 $word1[i:]$ 和 $word2[j:]$ 的大小,如果 $word1[i:]$ 比 $word2[j:]$ 大,那么我们就将 $word1[i]$ 加入答案,否则我们就将 $word2[j]$ 加入答案。循环,直至 $i$ 到达字符串 `word1` 的末尾,或者 $j$ 到达字符串 `word2` 的末尾。
67+
68+
然后我们将剩余的字符串加入答案即可。
69+
70+
时间复杂度 $O(n^2)$。其中 $n$ 是字符串 `word1``word2` 的长度之和。
71+
6672
<!-- tabs:start -->
6773

6874
### **Python3**
6975

7076
<!-- 这里可写当前语言的特殊实现逻辑 -->
7177

7278
```python
73-
79+
class Solution:
80+
def largestMerge(self, word1: str, word2: str) -> str:
81+
i = j = 0
82+
ans = []
83+
while i < len(word1) and j < len(word2):
84+
if word1[i:] > word2[j:]:
85+
ans.append(word1[i])
86+
i += 1
87+
else:
88+
ans.append(word2[j])
89+
j += 1
90+
ans.append(word1[i:])
91+
ans.append(word2[j:])
92+
return "".join(ans)
7493
```
7594

7695
### **Java**
7796

7897
<!-- 这里可写当前语言的特殊实现逻辑 -->
7998

8099
```java
100+
class Solution {
101+
public String largestMerge(String word1, String word2) {
102+
int m = word1.length(), n = word2.length();
103+
int i = 0, j = 0;
104+
StringBuilder ans = new StringBuilder();
105+
while (i < m && j < n) {
106+
boolean gt = word1.substring(i).compareTo(word2.substring(j)) > 0;
107+
ans.append(gt ? word1.charAt(i++) : word2.charAt(j++));
108+
}
109+
ans.append(word1.substring(i));
110+
ans.append(word2.substring(j));
111+
return ans.toString();
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
string largestMerge(string word1, string word2) {
122+
int m = word1.size(), n = word2.size();
123+
int i = 0, j = 0;
124+
string ans;
125+
while (i < m && j < n) {
126+
bool gt = word1.substr(i) > word2.substr(j);
127+
ans += gt ? word1[i++] : word2[j++];
128+
}
129+
ans += word1.substr(i);
130+
ans += word2.substr(j);
131+
return ans;
132+
}
133+
};
134+
```
81135
136+
### **Go**
137+
138+
```go
139+
func largestMerge(word1 string, word2 string) string {
140+
m, n := len(word1), len(word2)
141+
i, j := 0, 0
142+
var ans strings.Builder
143+
for i < m && j < n {
144+
if word1[i:] > word2[j:] {
145+
ans.WriteByte(word1[i])
146+
i++
147+
} else {
148+
ans.WriteByte(word2[j])
149+
j++
150+
}
151+
}
152+
ans.WriteString(word1[i:])
153+
ans.WriteString(word2[j:])
154+
return ans.String()
155+
}
82156
```
83157

84158
### **...**

‎solution/1700-1799/1754.Largest Merge Of Two Strings/README_EN.md

+69-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
<ul>
1010
<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-
1211
<ul>
1312
<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>
1413
</ul>
@@ -18,7 +17,6 @@
1817
<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>
1918
</ul>
2019
</li>
21-
2220
</ul>
2321

2422
<p>Return <em>the lexicographically <strong>largest</strong> </em><code>merge</code><em> you can construct</em>.</p>
@@ -62,13 +60,81 @@
6260
### **Python3**
6361

6462
```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)
6677
```
6778

6879
### **Java**
6980

7081
```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+
```
71117
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+
}
72138
```
73139

74140
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string largestMerge(string word1, string word2) {
4+
int m = word1.size(), n = word2.size();
5+
int i = 0, j = 0;
6+
string ans;
7+
while (i < m && j < n) {
8+
bool gt = word1.substr(i) > word2.substr(j);
9+
ans += gt ? word1[i++] : word2[j++];
10+
}
11+
ans += word1.substr(i);
12+
ans += word2.substr(j);
13+
return ans;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func largestMerge(word1 string, word2 string) string {
2+
m, n := len(word1), len(word2)
3+
i, j := 0, 0
4+
var ans strings.Builder
5+
for i < m && j < n {
6+
if word1[i:] > word2[j:] {
7+
ans.WriteByte(word1[i])
8+
i++
9+
} else {
10+
ans.WriteByte(word2[j])
11+
j++
12+
}
13+
}
14+
ans.WriteString(word1[i:])
15+
ans.WriteString(word2[j:])
16+
return ans.String()
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public String largestMerge(String word1, String word2) {
3+
int m = word1.length(), n = word2.length();
4+
int i = 0, j = 0;
5+
StringBuilder ans = new StringBuilder();
6+
while (i < m && j < n) {
7+
boolean gt = word1.substring(i).compareTo(word2.substring(j)) > 0;
8+
ans.append(gt ? word1.charAt(i++) : word2.charAt(j++));
9+
}
10+
ans.append(word1.substring(i));
11+
ans.append(word2.substring(j));
12+
return ans.toString();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def largestMerge(self, word1: str, word2: str) -> str:
3+
i = j = 0
4+
ans = []
5+
while i < len(word1) and j < len(word2):
6+
if word1[i:] > word2[j:]:
7+
ans.append(word1[i])
8+
i += 1
9+
else:
10+
ans.append(word2[j])
11+
j += 1
12+
ans.append(word1[i:])
13+
ans.append(word2[j:])
14+
return "".join(ans)

0 commit comments

Comments
 (0)
Please sign in to comment.