diff --git a/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/README.md b/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/README.md index f16a7d96b..c497f489b 100644 --- a/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/README.md +++ b/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/README.md @@ -1,28 +1,22 @@ # [1163.Last Substring in Lexicographical Order][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 +Given a string `s`, return the last substring of `s` in lexicographical order. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "abab" +Output: "bab" +Explanation: The substrings are ["a", "ab", "aba", "abab", "b", "ba", "bab"]. The lexicographically maximum substring is "bab". ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Last Substring in Lexicographical Order -```go ``` - +Input: s = "leetcode" +Output: "tcode" +``` ## 结语 diff --git a/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution.go b/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution.go index d115ccf5e..e555010f9 100644 --- a/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution.go +++ b/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) string { + indies := [26][]int{} + for i, b := range s { + indies[b-'a'] = append(indies[b-'a'], i) + } + i := 25 + for ; i >= 0 && len(indies[i]) == 0; i-- { + } + ans := "" + for _, index := range indies[i] { + ans = max(ans, s[index:]) + } + return ans } diff --git a/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution_test.go b/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution_test.go index 14ff50eb4..2c37db158 100644 --- a/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution_test.go +++ b/leetcode/1101-1200/1163.Last-Substring-in-Lexicographical-Order/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "abab", "bab"}, + {"TestCase2", "leetcode", "tcode"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }