diff --git a/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/README.md b/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/README.md index af73de22e..153393408 100755 --- a/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/README.md +++ b/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/README.md @@ -1,28 +1,41 @@ # [2337.Move Pieces to Obtain a String][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 `start` and `target`, both of length `n`. Each string consists **only** of the characters `'L'`, `'R'`, and `'_'` where: + +- The characters `'L'` and `'R'` represent pieces, where a piece `'L'` can move to the **left** only if there is a **blank** space directly to its left, and a piece `'R'` can move to the **right** only if there is a **blank** space directly to its right. +- The character `'_'` represents a blank space that can be occupied by **any** of the `'L'` or `'R'` pieces. + +Return `true` if it is possible to obtain the string `target` by moving the pieces of the string `start` **any** number of times. Otherwise, return `false`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: start = "_L__R__R_", target = "L______RR" +Output: true +Explanation: We can obtain the string target from start by doing the following moves: +- Move the first piece one step to the left, start becomes equal to "L___R__R_". +- Move the last piece one step to the right, start becomes equal to "L___R___R". +- Move the second piece three steps to the right, start becomes equal to "L______RR". +Since it is possible to get the string target from start, we return true. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Move Pieces to Obtain a String -```go ``` +Input: start = "R_L_", target = "__LR" +Output: false +Explanation: The 'R' piece in the string start can move one step to the right to obtain "_RL_". +After that, no pieces can move anymore, so it is impossible to obtain the string target from start. +``` + +**Example 3:** +``` +Input: start = "_R", target = "R_" +Output: false +Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start. +``` ## 结语 diff --git a/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution.go b/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution.go index d115ccf5e..e29e459cb 100644 --- a/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution.go +++ b/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution.go @@ -1,5 +1,42 @@ package Solution -func Solution(x bool) bool { - return x +type pair2337 struct { + c byte + i int +} + +func Solution(start string, target string) bool { + a, b := make([]pair2337, 0), make([]pair2337, 0) + for i, c := range []byte(start) { + if c == '_' { + continue + } + a = append(a, pair2337{c, i}) + } + for i, c := range []byte(target) { + if c == '_' { + continue + } + b = append(b, pair2337{c, i}) + } + if len(a) != len(b) { + // _ 不相等,无法转换 + return false + } + + for i := range len(a) { + ac := a[i] + bc := b[i] + if ac.c != bc.c { + return false + } + // 如果我此时是L + if ac.c == 'L' && ac.i < bc.i { + return false + } + if ac.c == 'R' && ac.i > bc.i { + return false + } + } + return true } diff --git a/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution_test.go b/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution_test.go index 14ff50eb4..5bc8b16c1 100644 --- a/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution_test.go +++ b/leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + start, target string + expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "_L__R__R_", "L______RR", true}, + {"TestCase2", "R_L_", "__LR", false}, + {"TestCase3", "_R", "R_", false}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.start, c.target) 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.start, c.target) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }