Skip to content

Commit

Permalink
Merge pull request #630 from 0xff-dev/720
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 720
  • Loading branch information
6boris authored Sep 24, 2023
2 parents 998e6d6 + d8033b5 commit 6a1879f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
27 changes: 13 additions & 14 deletions leetcode/701-800/0720.Longest-Word-in-Dictionary/README.md
Original file line number Diff line number Diff line change
@@ -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".
```

## 结语

Expand Down
27 changes: 25 additions & 2 deletions leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit 6a1879f

Please sign in to comment.