Skip to content

Commit f248b84

Browse files
refactor 591
1 parent 5fd697b commit f248b84

File tree

1 file changed

+1
-97
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+1
-97
lines changed

src/main/java/com/fishercoder/solutions/_591.java

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,8 @@
33
import java.util.ArrayDeque;
44
import java.util.Deque;
55

6-
/**
7-
* 591. Tag Validator
8-
*
9-
* Given a string representing a code snippet,
10-
* you need to implement a tag validator to parse the code and return whether it is valid.
11-
* A code snippet is valid if all the following rules hold:
12-
13-
1. The code must be wrapped in a valid closed tag. Otherwise, the code is invalid.
14-
15-
2. A closed tag (not necessarily valid) has exactly the following format : <TAG_NAME>TAG_CONTENT</TAG_NAME>.
16-
Among them, <TAG_NAME> is the start tag, and </TAG_NAME> is the end tag.
17-
The TAG_NAME in start and end tags should be the same.
18-
A closed tag is valid if and only if the TAG_NAME and TAG_CONTENT are valid.
19-
20-
3. A valid TAG_NAME only contain upper-case letters, and has length in range [1,9]. Otherwise, the TAG_NAME is invalid.
21-
22-
4. A valid TAG_CONTENT may contain other valid closed tags, cdata and any characters (see note1) EXCEPT unmatched <,
23-
unmatched start and end tag, and unmatched or closed tags with invalid TAG_NAME. Otherwise, the TAG_CONTENT is invalid.
24-
25-
5. A start tag is unmatched if no end tag exists with the same TAG_NAME, and vice versa.
26-
However, you also need to consider the issue of unbalanced when tags are nested.
27-
28-
29-
6. A < is unmatched if you cannot find a subsequent >. And when you find a < or </,
30-
all the subsequent characters until the next > should be parsed as TAG_NAME (not necessarily valid).
31-
32-
7. The cdata has the following format : <![CDATA[CDATA_CONTENT]]>.
33-
The range of CDATA_CONTENT is defined as the characters between <![CDATA[ and the first subsequent ]]>.
34-
35-
8. CDATA_CONTENT may contain any characters.
36-
The function of cdata is to forbid the validator to parse CDATA_CONTENT,
37-
so even it has some characters that can be parsed as tag (no matter valid or invalid), you should treat it as regular characters.
38-
39-
Valid Code Examples:
40-
Input: "<DIV>This is the first line <![CDATA[<div>]]></DIV>"
41-
42-
Output: True
43-
44-
Explanation:
45-
46-
The code is wrapped in a closed tag : <DIV> and </DIV>.
47-
48-
The TAG_NAME is valid, the TAG_CONTENT consists of some characters and cdata.
49-
50-
Although CDATA_CONTENT has unmatched start tag with invalid TAG_NAME, it should be considered as plain text, not parsed as tag.
51-
52-
So TAG_CONTENT is valid, and then the code is valid. Thus return true.
53-
54-
55-
Input: "<DIV>>> ![cdata[]] <![CDATA[<div>]>]]>]]>>]</DIV>"
56-
57-
Output: True
58-
59-
Explanation:
60-
61-
We first separate the code into : start_tag|tag_content|end_tag.
62-
63-
start_tag -> "<DIV>"
64-
65-
end_tag -> "</DIV>"
66-
67-
tag_content could also be separated into : text1|cdata|text2.
68-
69-
text1 -> ">> ![cdata[]] "
70-
71-
cdata -> "<![CDATA[<div>]>]]>", where the CDATA_CONTENT is "<div>]>"
72-
73-
text2 -> "]]>>]"
74-
75-
76-
The reason why start_tag is NOT "<DIV>>>" is because of the rule 6.
77-
The reason why cdata is NOT "<![CDATA[<div>]>]]>]]>" is because of the rule 7.
78-
Invalid Code Examples:
79-
Input: "<A> <B> </A> </B>"
80-
Output: False
81-
Explanation: Unbalanced. If "<A>" is closed, then "<B>" must be unmatched, and vice versa.
82-
83-
Input: "<DIV> div tag is not closed <DIV>"
84-
Output: False
85-
86-
Input: "<DIV> unmatched < </DIV>"
87-
Output: False
88-
89-
Input: "<DIV> closed tags with invalid tag name <b>123</b> </DIV>"
90-
Output: False
91-
92-
Input: "<DIV> unmatched tags with invalid tag name </1234567890> and <CDATA[[]]> </DIV>"
93-
Output: False
94-
95-
Input: "<DIV> unmatched start tag <B> and unmatched end tag </C> </DIV>"
96-
Output: False
97-
98-
Note:
99-
For simplicity, you could assume the input code (including the any characters mentioned above)
100-
only contain letters, digits, '<','>','/','!','[',']' and ' '.
101-
*/
1026
public class _591 {
103-
7+
1048
public static class Solution1 {
1059

10610
/**

0 commit comments

Comments
 (0)