-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path02-tag-validator.rs
51 lines (50 loc) · 1.98 KB
/
02-tag-validator.rs
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
// https://leetcode-cn.com/problems/tag-validator/
use std::collections::LinkedList;
impl Solution {
pub fn is_valid(mut code: String) -> bool {
let mut stack = LinkedList::new();
let mut code = code.as_bytes();
while !code.is_empty() {
if code[0] != b'<' {
if stack.is_empty() {
return false;
}
code = &code[1..];
continue;
}
if code.len() == 1 {
return false;
}
if code[1] == b'/' {
let j = code.iter().position(|&x| x == b'>');
if j.is_none() { return false; }
if stack.is_empty() || *stack.back().unwrap() != String::from_utf8_lossy(&code[2..j.unwrap()]) {
return false;
}
stack.pop_back();
code = &code[j.unwrap() + 1..];
if stack.is_empty() && !code.is_empty() { return false; }
} else if code[1] == b'!' {
if stack.is_empty() || code.len() < 9 || String::from_utf8_lossy(&code[2..9]) != "[CDATA[".to_owned() {
return false;
}
let j = String::from_utf8_lossy(code).find("]]>");
if j.is_none() { return false; }
code = &code[j.unwrap() + 1..];
} else {
let j = code.iter().position(|&x| x == b'>');
if j.is_none() { return false; }
let tag = String::from_utf8_lossy(&code[1..j.unwrap()]).to_string();
if tag.is_empty() || tag.len() > 9 { return false; }
for char in tag.chars() {
if !char.is_uppercase() {
return false;
}
}
stack.push_back(tag);
code = &code[j.unwrap() + 1..];
}
}
stack.is_empty()
}
}