Skip to content

Instantly share code, notes, and snippets.

@schaitanya
Created January 12, 2019 23:03
Show Gist options
  • Save schaitanya/489bd3d5d4ede07ad14c3cfd9c6a078d to your computer and use it in GitHub Desktop.
Save schaitanya/489bd3d5d4ede07ad14c3cfd9c6a078d to your computer and use it in GitHub Desktop.
import "strings"
// strs := []string{"flower", "flow", "flight"} -> fl
// strs := []string{"c", "acc", "ccc"} -> ""
func longestCommonPrefix(strs []string) string {
if len(strs) < 1 {
return ""
}
prefix := strs[0]
for i := 1; i < len(strs); i++ {
nStr := strs[i]
for len(prefix) > 0 && strings.Index(nStr, prefix) != 0 {
prefix = prefix[:len(prefix)-1]
}
}
return prefix
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment