Files
LeetCode-Go/leetcode/0720.Longest-Word-in-Dictionary/720. Longest Word in Dictionary.go
2020-08-07 17:06:53 +08:00

22 lines
318 B
Go

package leetcode
import (
"sort"
)
func longestWord(words []string) string {
sort.Strings(words)
mp := make(map[string]bool)
var res string
for _, word := range words {
size := len(word)
if size == 1 || mp[word[:size-1]] {
if size > len(res) {
res = word
}
mp[word] = true
}
}
return res
}