1impl Solution {2pub fn is_valid(code: String) -> bool {3Self::_is_valid(code.as_bytes()).unwrap_or(false)4}56fn _is_valid(code: &[u8]) -> Option<bool> {7let mut s = String::new();8let mut i = 0;9while i < code.len() {10if code[i] == b'<' && code.get(i + 1)? == &b'!' {11let mut t = String::new();12for (j, &item) in code.iter().enumerate().skip(i + 2) {13if j - i == 9 {14break;15}16t.push(item as char);17}18if t == "[CDATA[" || t == "[cdata[" {19i += 7;20while i < code.len() {21if code[i - 2] == b']' && code[i - 1] == b']' && code[i] == b'>' {22break;23}24i += 1;25}26s.push('#');27} else {28return Some(false);29}30} else {31s.push(code[i] as char);32}33i += 1;34}3536let s = s.as_bytes();3738if s.is_empty() || s[0] != b'<' {39return Some(false);40}4142let mut st = Vec::new();43let mut k = 0;44let mut n = 0;45i = 0;46while i < s.len() {47if s[i] == b'<' {48let mut t = String::new();49n = 0;50if i == s.len() {51return Some(false);52}5354if s.get(i + 1)? == &b'/' {55i += 2;56while i < s.len() {57let &s_i_byte = s.get(i)?;58if s_i_byte == b'>' {59break;60}61if !(b'A'..=b'Z').contains(&s_i_byte) {62return Some(false);63}64t.push(s_i_byte as char);65if t.len() == 10 {66return Some(false);67}68i += 1;69}7071if st.is_empty() || st.pop().unwrap() != t {72return Some(false);73}74} else {75i += 1;76while i < s.len() {77let &s_i_byte = s.get(i)?;78if s_i_byte == b'>' {79break;80}81if !(b'A'..=b'Z').contains(&s_i_byte) {82return Some(false);83}84t.push(s_i_byte as char);85if t.len() == 10 {86return Some(false);87}88i += 1;89}9091if t.is_empty()92|| (st.is_empty() && {93k += 1;94k == 295})96{97return Some(false);98}99st.push(t);100}101} else {102n = 1;103}104i += 1;105}106Some(st.is_empty() && n == 0)107}108}