Files
LeetCode-Go/leetcode/0014.Longest-Common-Prefix/14.Longest Common Prefix.go
2021-12-20 20:47:59 -03:00

17 lines
276 B
Go

package leetcode
func longestCommonPrefix(strs []string) string {
prefix := strs[0]
for i := 1; i < len(strs); i++ {
for j := 0; j < len(prefix); j++ {
if len(strs[i]) <= j || strs[i][j] != prefix[j] {
prefix = prefix[0:j]
break
}
}
}
return prefix
}