-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (54 loc) · 1.25 KB
/
main.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
func isLongPressedName(name, typed string) bool {
pnt1, pnt2 := 0, 0
for pnt1 < len(name) && pnt2 < len(typed) {
ncount := 1
for pnt1 < len(name)-1 && name[pnt1] == name[pnt1+1] {
pnt1, ncount = pnt1+1, ncount+1
}
tcount := 1
for pnt2 < len(typed)-1 && typed[pnt2] == typed[pnt2+1] {
pnt2, tcount = pnt2+1, tcount+1
}
if name[pnt1] != typed[pnt2] || ncount > tcount {
return false
}
pnt1, pnt2 = pnt1+1, pnt2+1
}
return pnt1 == len(name) && pnt2 == len(typed)
}
func isLongPressedName2(name, typed string) bool {
pnt1, pnt2 := 0, 0
nLen, tLen := len(name), len(typed)
for pnt1 < nLen && pnt2 < tLen {
char := name[pnt1]
if char != typed[pnt2] {
return false
}
var ncount int
for pnt1 < nLen && name[pnt1] == char {
ncount, pnt1 = ncount+1, pnt1+1
}
var tcount int
for pnt2 < tLen && typed[pnt2] == char {
tcount, pnt2 = tcount+1, pnt2+1
}
if ncount > tcount {
return false
}
}
return pnt1 == nLen && pnt2 == tLen
}
func isLongPressedName3(name, typed string) bool {
pnt, nLen := 0, len(name)
var prev byte
for i := 0; i < len(typed); i++ {
if pnt < nLen && name[pnt] == typed[i] {
pnt++
prev = typed[i]
} else if prev != typed[i] {
return false
}
}
return pnt == nLen
}