Skip to content

Commit 46c378d

Browse files
committed
Add solution and test-cases for problem 1754
1 parent 7ce34f6 commit 46c378d

File tree

3 files changed

+76
-27
lines changed

3 files changed

+76
-27
lines changed

leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/README.md

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,40 @@
11
# [1754.Largest Merge Of Two Strings][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given two strings `word1` and `word2`. You want to construct a string `merge` in the following way: while either `word1` or `word2` are non-empty, choose **one** of the following options:
5+
6+
- If `word1` is non-empty, append the **first** character in `word1` to `merge` and delete it from `word1`.
7+
8+
- For example, if `word1 = "abc"` and `merge = "dv"`, then after choosing this operation, `word1 = "bc"` and `merge = "dva"`.
9+
10+
- If `word2` is non-empty, append the **first** character in `word2` to `merge` and delete it from `word2`.
11+
12+
- For example, if `word2 = "abc"` and `merge = ""`, then after choosing this operation, `word2 = "bc"` and `merge = "a"`.
13+
14+
Return the lexicographically **largest** merge you can construct.
15+
16+
A string `a` is lexicographically larger than a string `b` (of the same length) if in the first position where `a` and `b` differ, a has a character strictly larger than the corresponding character in `b`. For example, `"abcd"` is lexicographically larger than `"abcc"` because the first position they differ is at the fourth character, and `d` is greater than `c`.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: word1 = "cabaa", word2 = "bcaaa"
22+
Output: "cbcabaaaaa"
23+
Explanation: One way to get the lexicographically largest merge is:
24+
- Take from word1: merge = "c", word1 = "abaa", word2 = "bcaaa"
25+
- Take from word2: merge = "cb", word1 = "abaa", word2 = "caaa"
26+
- Take from word2: merge = "cbc", word1 = "abaa", word2 = "aaa"
27+
- Take from word1: merge = "cbca", word1 = "baa", word2 = "aaa"
28+
- Take from word1: merge = "cbcab", word1 = "aa", word2 = "aaa"
29+
- Append the remaining 5 a's from word1 and word2 at the end of merge.
1330
```
1431

15-
## 题意
16-
> ...
17-
18-
## 题解
32+
**Example 2:**
1933

20-
### 思路1
21-
> ...
22-
Largest Merge Of Two Strings
23-
```go
2434
```
25-
35+
Input: word1 = "abcabc", word2 = "abdcaba"
36+
Output: "abdcabcabcaba"
37+
```
2638

2739
## 结语
2840

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "bytes"
4+
5+
func Solution(word1 string, word2 string) string {
6+
buf := bytes.Buffer{}
7+
i, j := 0, 0
8+
9+
for i < len(word1) && j < len(word2) {
10+
selectI := false
11+
if word1[i] == word2[j] {
12+
// 相等情况
13+
ii, jj := i+1, j+1
14+
for ; ii < len(word1) && jj < len(word2) && word1[ii] == word2[jj]; ii, jj = ii+1, jj+1 {
15+
}
16+
if ii == len(word1) {
17+
selectI = false
18+
} else if jj == len(word2) {
19+
selectI = true
20+
} else {
21+
selectI = word1[ii] > word2[jj]
22+
}
23+
}
24+
25+
if selectI || word1[i] > word2[j] {
26+
buf.WriteByte(word1[i])
27+
i++
28+
continue
29+
}
30+
if !selectI || word1[i] < word2[j] {
31+
buf.WriteByte(word2[j])
32+
j++
33+
continue
34+
}
35+
}
36+
for ; i < len(word1); i++ {
37+
buf.WriteByte(word1[i])
38+
}
39+
for ; j < len(word2); j++ {
40+
buf.WriteByte(word2[j])
41+
}
42+
return buf.String()
543
}

leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
word1, word2 string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "cabaa", "bcaaa", "cbcabaaaaa"},
17+
{"TestCase2", "abcabc", "abdcaba", "abdcabcabcaba"},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.word1, c.word2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.word1, c.word2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)