12345678910111213141516171819202122232425262728293031323334 |
- type strslice []string
- func (ss strslice) Len() int { return len(ss) }
- func (ss strslice) Less(i, j int) bool {
- if m, n := len(ss[i]), len(ss[j]); m != n {
- return n < m
- }
- return ss[i] < ss[j]
- }
- func (ss strslice) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] }
- func findLongestWord(s string, d []string) string {
- sort.Sort(strslice(d))
- for i := range d {
- if isSubstr(s, d[i]) {
- return d[i]
- }
- }
- return ""
- }
- func isSubstr(s, t string) bool {
- i, j, m, n := 0, 0, len(s), len(t)
- if m < n {
- return false
- }
- s1, s2 := []rune(s), []rune(t)
- for ; i < m && j < n; j++ {
- for ; i < m && s1[i] != s2[j]; i++ {
- }
- i++
- }
- return i <= m && j == n
- }
|