Skip to content

Commit 9abaabc

Browse files
committed
Merge pull request #201 from bumblebee211196/develop
✨ Add solution and testcases to problem 791
2 parents 1260afb + 267715d commit 9abaabc

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

leetcode/701-800/0791.Custom-Sort-String/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# [791.Custom Sort 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
74

5+
order and str are strings composed of lowercase letters. In order, no letter occurs more than once.
6+
7+
order was sorted in some custom order previously. We want to permute the characters of str so that they match the order that order was sorted. More specifically, if x occurs before y in order, then x should occur before y in the returned string.
8+
9+
Return any permutation of str (as a string) that satisfies this property.
10+
811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input:
15+
order = "cba"
16+
str = "abcd"
17+
Output: "cbad"
1318
```
1419

1520
## 题意
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(order string, str string) string {
6+
mapper := make(map[byte]int)
7+
for i:= range order {
8+
mapper[order[i]] = i - 26
9+
}
10+
res := []byte(str)
11+
sort.Slice(res, func(i, j int) bool {
12+
return mapper[res[i]] < mapper[res[j]]
13+
})
14+
return string(res)
515
}

leetcode/701-800/0791.Custom-Sort-String/Solution_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
order string
14+
str string
15+
expect string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase", "cba", "abcd", "cbad"},
18+
{"TestCase", "bee", "bumblebee", "bbbeeelum"},
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.order, c.str)
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.order, c.str)
2828
}
2929
})
3030
}

0 commit comments

Comments
 (0)