123456789101112131415161718192021222324252627282930 |
- package main
- func longestCommonPrefix(strs []string) string {
- res := make([]rune, 0)
- // spacial case
- if len(res) == 0 {
- return ""
- }
- cnt := len(strs[0])
- // find minimum length of strings
- for _, v := range strs {
- if cnt > len(v) {
- cnt = len(v)
- }
- }
- // vertical serach
- // [0 _] [0 _] [0 _] --> [_ 1] [_ 1] [_ 0] --> [0]
- for i := 0; i < cnt; i++ {
- curr := strs[0][i]
- for _, v := range strs {
- if curr != v[i] {
- cnt = -1
- }
- }
- if cnt != -1 {
- res = append(res, rune(curr))
- }
- }
- return string(res)
- }
|