lc-go/solutions/0/q14/solution.go

20 lines
459 B
Go

// Package q14 implements a solution for https://leetcode.com/problems/longest-common-prefix/
package q14
func longestCommonPrefix(strs []string) string {
minLen := len(strs[0])
for i := 1; i < len(strs); i++ {
minLen = min(minLen, len(strs[i]))
}
for pfxLen := range minLen {
for i := 1; i < len(strs); i++ {
if strs[i][pfxLen] != strs[0][pfxLen] {
return strs[0][:pfxLen]
}
}
}
return strs[0][:minLen]
}
var _ = longestCommonPrefix