diff --git a/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/README.md b/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/README.md index 74dd14312..6108804a8 100755 --- a/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/README.md +++ b/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/README.md @@ -1,28 +1,40 @@ # [1754.Largest Merge Of Two Strings][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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: + +- If `word1` is non-empty, append the **first** character in `word1` to `merge` and delete it from `word1`. + + - For example, if `word1 = "abc"` and `merge = "dv"`, then after choosing this operation, `word1 = "bc"` and `merge = "dva"`. + +- If `word2` is non-empty, append the **first** character in `word2` to `merge` and delete it from `word2`. + + - For example, if `word2 = "abc"` and `merge = ""`, then after choosing this operation, `word2 = "bc"` and `merge = "a"`. + +Return the lexicographically **largest** merge you can construct. + +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`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: word1 = "cabaa", word2 = "bcaaa" +Output: "cbcabaaaaa" +Explanation: One way to get the lexicographically largest merge is: +- Take from word1: merge = "c", word1 = "abaa", word2 = "bcaaa" +- Take from word2: merge = "cb", word1 = "abaa", word2 = "caaa" +- Take from word2: merge = "cbc", word1 = "abaa", word2 = "aaa" +- Take from word1: merge = "cbca", word1 = "baa", word2 = "aaa" +- Take from word1: merge = "cbcab", word1 = "aa", word2 = "aaa" +- Append the remaining 5 a's from word1 and word2 at the end of merge. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Largest Merge Of Two Strings -```go ``` - +Input: word1 = "abcabc", word2 = "abdcaba" +Output: "abdcabcabcaba" +``` ## 结语 diff --git a/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution.go b/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution.go index d115ccf5e..7f3365f25 100644 --- a/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution.go +++ b/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution.go @@ -1,5 +1,43 @@ package Solution -func Solution(x bool) bool { - return x +import "bytes" + +func Solution(word1 string, word2 string) string { + buf := bytes.Buffer{} + i, j := 0, 0 + + for i < len(word1) && j < len(word2) { + selectI := false + if word1[i] == word2[j] { + // 相等情况 + ii, jj := i+1, j+1 + for ; ii < len(word1) && jj < len(word2) && word1[ii] == word2[jj]; ii, jj = ii+1, jj+1 { + } + if ii == len(word1) { + selectI = false + } else if jj == len(word2) { + selectI = true + } else { + selectI = word1[ii] > word2[jj] + } + } + + if selectI || word1[i] > word2[j] { + buf.WriteByte(word1[i]) + i++ + continue + } + if !selectI || word1[i] < word2[j] { + buf.WriteByte(word2[j]) + j++ + continue + } + } + for ; i < len(word1); i++ { + buf.WriteByte(word1[i]) + } + for ; j < len(word2); j++ { + buf.WriteByte(word2[j]) + } + return buf.String() } diff --git a/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution_test.go b/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution_test.go index 14ff50eb4..36e92291e 100644 --- a/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution_test.go +++ b/leetcode/1701-1800/1754.Largest-Merge-Of-Two-Strings/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + word1, word2 string + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "cabaa", "bcaaa", "cbcabaaaaa"}, + {"TestCase2", "abcabc", "abdcaba", "abdcabcabcaba"}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.word1, c.word2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.word1, c.word2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }