Skip to content

Commit caa2fd8

Browse files
authored
Merge pull request #1052 from 0xff-dev/2337
Add solution and test-cases for problem 2337
2 parents 6cf0049 + f8ed0db commit caa2fd8

File tree

3 files changed

+76
-26
lines changed

3 files changed

+76
-26
lines changed

leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/README.md

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [2337.Move Pieces to Obtain a String][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 `start` and `target`, both of length `n`. Each string consists **only** of the characters `'L'`, `'R'`, and `'_'` where:
5+
6+
- 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.
7+
- The character `'_'` represents a blank space that can be occupied by **any** of the `'L'` or `'R'` pieces.
8+
9+
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`.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: start = "_L__R__R_", target = "L______RR"
15+
Output: true
16+
Explanation: We can obtain the string target from start by doing the following moves:
17+
- Move the first piece one step to the left, start becomes equal to "L___R__R_".
18+
- Move the last piece one step to the right, start becomes equal to "L___R___R".
19+
- Move the second piece three steps to the right, start becomes equal to "L______RR".
20+
Since it is possible to get the string target from start, we return true.
1321
```
1422

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

20-
### 思路1
21-
> ...
22-
Move Pieces to Obtain a String
23-
```go
2425
```
26+
Input: start = "R_L_", target = "__LR"
27+
Output: false
28+
Explanation: The 'R' piece in the string start can move one step to the right to obtain "_RL_".
29+
After that, no pieces can move anymore, so it is impossible to obtain the string target from start.
30+
```
31+
32+
**Example 3:**
2533

34+
```
35+
Input: start = "_R", target = "R_"
36+
Output: false
37+
Explanation: The piece in the string start can move only to the right, so it is impossible to obtain the string target from start.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
type pair2337 struct {
4+
c byte
5+
i int
6+
}
7+
8+
func Solution(start string, target string) bool {
9+
a, b := make([]pair2337, 0), make([]pair2337, 0)
10+
for i, c := range []byte(start) {
11+
if c == '_' {
12+
continue
13+
}
14+
a = append(a, pair2337{c, i})
15+
}
16+
for i, c := range []byte(target) {
17+
if c == '_' {
18+
continue
19+
}
20+
b = append(b, pair2337{c, i})
21+
}
22+
if len(a) != len(b) {
23+
// _ 不相等,无法转换
24+
return false
25+
}
26+
27+
for i := range len(a) {
28+
ac := a[i]
29+
bc := b[i]
30+
if ac.c != bc.c {
31+
return false
32+
}
33+
// 如果我此时是L
34+
if ac.c == 'L' && ac.i < bc.i {
35+
return false
36+
}
37+
if ac.c == 'R' && ac.i > bc.i {
38+
return false
39+
}
40+
}
41+
return true
542
}

leetcode/2301-2400/2337.Move-Pieces-to-Obtain-a-String/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
start, target string
14+
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "_L__R__R_", "L______RR", true},
17+
{"TestCase2", "R_L_", "__LR", false},
18+
{"TestCase3", "_R", "R_", false},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.start, c.target)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.start, c.target)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)