You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
366 B

package longestprefix
import (
"strings"
)
func LongestCommonPrefix(strs []string) string {
result := ""
if len(strs) == 0 {
return result
}
first := strs[0]
rest := strs[1:]
for i := range first {
prefix := first[:i+1]
for _, s := range rest {
if !strings.HasPrefix(s, prefix) {
return result
}
}
result = prefix
}
return first
}