522.longest-uncommon-subsequence-ii.go 767 B

1234567891011121314151617181920212223242526272829303132333435
  1. type strslice []string
  2. func (ss strslice) Len() int { return len(ss) }
  3. func (ss strslice) Less(i, j int) bool { return len(ss[j]) < len(ss[i]) }
  4. func (ss strslice) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] }
  5. func findLUSlength(strs []string) int {
  6. sort.Sort(strslice(strs))
  7. m := make(map[string]int)
  8. for i := range strs {
  9. m[strs[i]]++
  10. }
  11. for i := range strs {
  12. if val, _ := m[strs[i]]; val == 1 {
  13. j := 0
  14. for ; j < i && !isSubstr(strs[j], strs[i]); j++ {
  15. }
  16. if j == i {
  17. return len(strs[i])
  18. }
  19. }
  20. }
  21. return -1
  22. }
  23. func isSubstr(s, t string) bool {
  24. i, j, m, n := 0, 0, len(s), len(t)
  25. s1, s2 := []rune(s), []rune(t)
  26. for ; i < m && j < n; j++ {
  27. for ; i < m && s1[i] != s2[j]; i++ {
  28. }
  29. i++
  30. }
  31. return i <= m && j == n
  32. }