-
Notifications
You must be signed in to change notification settings - Fork 0
/
591.go
56 lines (53 loc) · 1.03 KB
/
591.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
package p591
import "strings"
func isValid(code string) bool {
stack := make([]string, 0)
i := 0
for i < len(code) {
if i > 0 && len(stack) == 0 {
return false
}
if strings.HasPrefix(code[i:], "<![CDATA[") {
j := i + 9
i = strings.Index(code[j:], "]]>") + j
if i < j {
return false
}
i += 3
} else if strings.HasPrefix(code[i:], "</") {
j := i + 2
i = strings.Index(code[j:], ">") + j
if i <= j || i-j > 9 {
return false
}
for k := j; k < i; k++ {
if code[k] > 'Z' || code[k] < 'A' {
return false
}
}
s := code[j:i]
i++
if len(stack) == 0 || s != stack[len(stack)-1] {
return false
}
stack = stack[:len(stack)-1]
} else if strings.HasPrefix(code[i:], "<") {
j := i + 1
i = strings.Index(code[j:], ">") + j
if i <= j || i-j > 9 {
return false
}
for k := j; k < i; k++ {
if code[k] > 'Z' || code[k] < 'A' {
return false
}
}
s := code[j:i]
i++
stack = append(stack, s)
} else {
i++
}
}
return len(stack) == 0
}