-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #630 from 0xff-dev/720
Add solution and test-cases for problem 720
- Loading branch information
Showing
3 changed files
with
44 additions
and
23 deletions.
There are no files selected for viewing
27 changes: 13 additions & 14 deletions
27
leetcode/701-800/0720.Longest-Word-in-Dictionary/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 25 additions & 2 deletions
27
leetcode/701-800/0720.Longest-Word-in-Dictionary/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters