题目:
题解:
func findLongestWord(s string, dictionary []string) (ans string) {
m := len(s)
f := make([][26]int, m+1)
for i := range f[m] {
f[m][i] = m
}
for i := m - 1; i >= 0; i-- {
f[i] = f[i+1]
f[i][s[i]-'a'] = i
}
outer:
for _, t := range dictionary {
j := 0
for _, ch := range t {
if f[j][ch-'a'] == m {
continue outer
}
j = f[j][ch-'a'] + 1
}
if len(t) > len(ans) || len(t) == len(ans) && t < ans {
ans = t
}
}
return
}