forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path591.Tag-Validator_s2.cpp
More file actions
70 lines (61 loc) · 1.99 KB
/
Copy path591.Tag-Validator_s2.cpp
File metadata and controls
70 lines (61 loc) · 1.99 KB
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
62
63
64
65
66
67
68
69
70
class Solution {
public:
bool isValid(string code)
{
stack<string>Stack;
int i=0;
bool ever = 0;
while (i<code.size())
{
if (i+8<code.size() && code.substr(i,9)=="<![CDATA[")
{
i+=9;
int i0=i;
while (i+2<code.size() && code.substr(i,3)!="]]>")
i++;
if (i+2==code.size()) return false;
i+=3;
}
else if (i+1<code.size() && code.substr(i,2)=="</")
{
i+=2;
int i0=i;
while (i<code.size() && code[i]!='>')
i++;
if (i==code.size()) return false;
string tagname = code.substr(i0,i-i0);
if (Stack.empty() || Stack.top()!=tagname)
return false;
Stack.pop();
i++;
if (Stack.empty() && i!=code.size()) return false;
}
else if (code[i]=='<')
{
i++;
int i0=i;
while (i<code.size() && code[i]!='>')
i++;
if (i==code.size()) return false;
string tagname = code.substr(i0,i-i0);
if (!isValidTag(tagname)) return false;
if (ever==false && i0!=1) return false;
ever = true;
Stack.push(tagname);
i++;
}
else
i++;
}
if (!Stack.empty()) return false;
if (ever==0) return false;
return true;
}
bool isValidTag(string tagname)
{
if (tagname.size()<1 || tagname.size()>9) return false;
for (auto ch:tagname)
if (ch<'A'|| ch>'Z') return false;
return true;
}
};