-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution14.go
More file actions
38 lines (36 loc) · 730 Bytes
/
solution14.go
File metadata and controls
38 lines (36 loc) · 730 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package solution14
// ============================================================================
// 14. Longest Common Prefix
// URL: https://leetcode.com/problems/longest-common-prefix/
// ============================================================================
func longestCommonPrefix(strs []string) string {
length := len(strs)
prefix := make([]byte, 0, length)
var char byte
var c, i, l int
for {
c = 0
for _, str := range strs {
l = len(str)
if i >= l {
return string(prefix)
}
if char == 0 {
char = str[i]
}
if char == str[i] {
c++
}
}
if c != length {
break
}
prefix = append(prefix, char)
char = 0
i++
if i > l {
break
}
}
return string(prefix)
}