diff --git a/leetcode/701-800/0720.Longest-Word-in-Dictionary/README.md b/leetcode/701-800/0720.Longest-Word-in-Dictionary/README.md index f6d80ebda..60ed54e1d 100644 --- a/leetcode/701-800/0720.Longest-Word-in-Dictionary/README.md +++ b/leetcode/701-800/0720.Longest-Word-in-Dictionary/README.md @@ -1,28 +1,27 @@ # [720.Longest Word in Dictionary][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 an array of strings `words` representing an English Dictionary, return the longest word in `words` that can be built one character at a time by other `words` in words. + +If there is more than one possible answer, return the longest word with the smallest lexicographical order. If there is no answer, return the empty string. + +Note that the word should be built from left to right with each additional character being added to the end of a previous word. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: words = ["w","wo","wor","worl","world"] +Output: "world" +Explanation: The word "world" can be built one character at a time by "w", "wo", "wor", and "worl". ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Longest Word in Dictionary -```go ``` - +Input: words = ["a","banana","app","appl","ap","apply","apple"] +Output: "apple" +Explanation: Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply". +``` ## 结语 diff --git a/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution.go b/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution.go index d115ccf5e..ab2f80b59 100644 --- a/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution.go +++ b/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +import "sort" + +func Solution(words []string) string { + sort.Slice(words, func(i, j int) bool { + li, lj := len(words[i]), len(words[j]) + if li == lj { + return words[i] < words[j] + } + return li < lj + }) + ok := make(map[string]struct{}) + ans := "" + ok[ans] = struct{}{} + for _, w := range words { + lp := len(w) + pre := w[:lp-1] + if _, nice := ok[pre]; nice { + ok[w] = struct{}{} + if lp > len(ans) || (lp == len(ans) && w < ans) { + ans = w + } + } + } + + return ans } diff --git a/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution_test.go b/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution_test.go index 14ff50eb4..45a098d91 100644 --- a/leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution_test.go +++ b/leetcode/701-800/0720.Longest-Word-in-Dictionary/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", []string{"a", "banana", "app", "appl", "ap", "apply", "apple"}, "apple"}, + {"TestCase2", []string{"w", "wo", "wor", "worl", "world"}, "world"}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }