Skip to content

Commit 7897084

Browse files
authored
Merge pull request #1225 from 0xff-dev/1163
Add solution and test-cases for problem 1163
2 parents 49e3a5f + 27cfe2f commit 7897084

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
# [1163.Last Substring in Lexicographical Order][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+
Given a string `s`, return the last substring of `s` in lexicographical order.
75

86
**Example 1:**
97

108
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
9+
Input: s = "abab"
10+
Output: "bab"
11+
Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab".
1312
```
1413

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

20-
### 思路1
21-
> ...
22-
Last Substring in Lexicographical Order
23-
```go
2416
```
25-
17+
Input: s = "leetcode"
18+
Output: "tcode"
19+
```
2620

2721
## 结语
2822

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) string {
4+
indies := [26][]int{}
5+
for i, b := range s {
6+
indies[b-'a'] = append(indies[b-'a'], i)
7+
}
8+
i := 25
9+
for ; i >= 0 && len(indies[i]) == 0; i-- {
10+
}
11+
ans := ""
12+
for _, index := range indies[i] {
13+
ans = max(ans, s[index:])
14+
}
15+
return ans
516
}

leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abab", "bab"},
17+
{"TestCase2", "leetcode", "tcode"},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

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

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

0 commit comments

Comments
 (0)