1234567891011121314151617181920212223242526272829303132333435 |
- type strslice []string
- func (ss strslice) Len() int { return len(ss) }
- func (ss strslice) Less(i, j int) bool { return len(ss[j]) < len(ss[i]) }
- func (ss strslice) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] }
- func findLUSlength(strs []string) int {
- sort.Sort(strslice(strs))
- m := make(map[string]int)
- for i := range strs {
- m[strs[i]]++
- }
- for i := range strs {
- if val, _ := m[strs[i]]; val == 1 {
- j := 0
- for ; j < i && !isSubstr(strs[j], strs[i]); j++ {
- }
- if j == i {
- return len(strs[i])
- }
- }
- }
- return -1
- }
- func isSubstr(s, t string) bool {
- i, j, m, n := 0, 0, len(s), len(t)
- 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
- }
|