Skip to content

Commit 5e1ff0b

Browse files
committed
feat: add solutions to lc problem: No.0591
No.0591.Tag Validator
1 parent 7c193b8 commit 5e1ff0b

File tree

9 files changed

+432
-7
lines changed

9 files changed

+432
-7
lines changed

solution/0500-0599/0591.Tag Validator/README.md

+140-1
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,161 @@ cdata <strong>不</strong>是 <strong>&quot;&lt;![CDATA[&lt;div&gt;]&gt;]]&gt;]]
9595

9696
<!-- 这里可写通用的实现逻辑 -->
9797

98+
**方法一:栈模拟**
99+
98100
<!-- tabs:start -->
99101

100102
### **Python3**
101103

102104
<!-- 这里可写当前语言的特殊实现逻辑 -->
103105

104106
```python
105-
107+
class Solution:
108+
def isValid(self, code: str) -> bool:
109+
def check(tag):
110+
n = len(tag)
111+
if n < 1 or n > 9:
112+
return False
113+
return all(c.isupper() for c in tag)
114+
115+
stk = []
116+
i, n = 0, len(code)
117+
while i < n:
118+
if i and not stk:
119+
return False
120+
if code[i: i + 9] == '<![CDATA[':
121+
i = code.find(']]>', i + 9)
122+
if i < 0:
123+
return False
124+
i += 2
125+
elif code[i: i + 2] == '</':
126+
j = i + 2
127+
i = code.find('>', j)
128+
if i < 0:
129+
return False
130+
t = code[j: i]
131+
if not check(t) or not stk or stk[-1] != t:
132+
return False
133+
stk.pop()
134+
elif code[i] == '<':
135+
j = i + 1
136+
i = code.find('>', j)
137+
if i < 0:
138+
return False
139+
t = code[j: i]
140+
if not check(t):
141+
return False
142+
stk.append(t)
143+
i += 1
144+
return not stk
106145
```
107146

108147
### **Java**
109148

110149
<!-- 这里可写当前语言的特殊实现逻辑 -->
111150

112151
```java
152+
class Solution {
153+
public boolean isValid(String code) {
154+
Deque<String> stk = new ArrayDeque<>();
155+
for (int i = 0; i < code.length(); ++i) {
156+
if (i > 0 && stk.isEmpty()) {
157+
return false;
158+
}
159+
if (code.startsWith("<![CDATA[", i)) {
160+
i = code.indexOf("]]>", i + 9);
161+
if (i < 0) {
162+
return false;
163+
}
164+
i += 2;
165+
} else if (code.startsWith("</", i)) {
166+
int j = i + 2;
167+
i = code.indexOf(">", j);
168+
if (i < 0) {
169+
return false;
170+
}
171+
String t = code.substring(j, i);
172+
if (!check(t) || stk.isEmpty() || !stk.pop().equals(t)) {
173+
return false;
174+
}
175+
} else if (code.startsWith("<", i)) {
176+
int j = i + 1;
177+
i = code.indexOf(">", j);
178+
if (i < 0) {
179+
return false;
180+
}
181+
String t = code.substring(j, i);
182+
if (!check(t)) {
183+
return false;
184+
}
185+
stk.push(t);
186+
}
187+
}
188+
return stk.isEmpty();
189+
}
190+
191+
private boolean check(String tag) {
192+
int n = tag.length();
193+
if (n < 1 || n > 9) {
194+
return false;
195+
}
196+
for (char c : tag.toCharArray()) {
197+
if (!Character.isUpperCase(c)) {
198+
return false;
199+
}
200+
}
201+
return true;
202+
}
203+
}
204+
```
113205

206+
### **C++**
207+
208+
```cpp
209+
class Solution {
210+
public:
211+
bool isValid(string code) {
212+
stack<string> stk;
213+
for (int i = 0; i < code.size(); ++i)
214+
{
215+
if (i && stk.empty()) return false;
216+
if (code.substr(i, 9) == "<![CDATA[")
217+
{
218+
i = code.find("]]>", i + 9);
219+
if (i < 0) return false;
220+
i += 2;
221+
}
222+
else if (code.substr(i, 2) == "</")
223+
{
224+
int j = i + 2;
225+
i = code.find('>', j);
226+
if (i < 0) return false;
227+
string t = code.substr(j, i - j);
228+
if (!check(t) || stk.empty() || stk.top() != t) return false;
229+
stk.pop();
230+
}
231+
else if (code.substr(i, 1) == "<")
232+
{
233+
int j = i + 1;
234+
i = code.find('>', j);
235+
if (i < 0) return false;
236+
string t = code.substr(j, i - j);
237+
if (!check(t)) return false;
238+
stk.push(t);
239+
}
240+
}
241+
return stk.empty();
242+
}
243+
244+
bool check(string tag) {
245+
int n = tag.size();
246+
if (n < 1 || n > 9) return false;
247+
for (char& c : tag)
248+
if (!isupper(c))
249+
return false;
250+
return true;
251+
}
252+
};
114253
```
115254

116255
### **...**

solution/0500-0599/0591.Tag Validator/README_EN.md

+138-1
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,150 @@ The reason why cdata is NOT <b>&quot;&lt;![CDATA[&lt;div&gt;]&gt;]]&gt;]]&gt;&qu
7272
### **Python3**
7373

7474
```python
75-
75+
class Solution:
76+
def isValid(self, code: str) -> bool:
77+
def check(tag):
78+
n = len(tag)
79+
if n < 1 or n > 9:
80+
return False
81+
return all(c.isupper() for c in tag)
82+
83+
stk = []
84+
i, n = 0, len(code)
85+
while i < n:
86+
if i and not stk:
87+
return False
88+
if code[i: i + 9] == '<![CDATA[':
89+
i = code.find(']]>', i + 9)
90+
if i < 0:
91+
return False
92+
i += 2
93+
elif code[i: i + 2] == '</':
94+
j = i + 2
95+
i = code.find('>', j)
96+
if i < 0:
97+
return False
98+
t = code[j: i]
99+
if not check(t) or not stk or stk[-1] != t:
100+
return False
101+
stk.pop()
102+
elif code[i] == '<':
103+
j = i + 1
104+
i = code.find('>', j)
105+
if i < 0:
106+
return False
107+
t = code[j: i]
108+
if not check(t):
109+
return False
110+
stk.append(t)
111+
i += 1
112+
return not stk
76113
```
77114

78115
### **Java**
79116

80117
```java
118+
class Solution {
119+
public boolean isValid(String code) {
120+
Deque<String> stk = new ArrayDeque<>();
121+
for (int i = 0; i < code.length(); ++i) {
122+
if (i > 0 && stk.isEmpty()) {
123+
return false;
124+
}
125+
if (code.startsWith("<![CDATA[", i)) {
126+
i = code.indexOf("]]>", i + 9);
127+
if (i < 0) {
128+
return false;
129+
}
130+
i += 2;
131+
} else if (code.startsWith("</", i)) {
132+
int j = i + 2;
133+
i = code.indexOf(">", j);
134+
if (i < 0) {
135+
return false;
136+
}
137+
String t = code.substring(j, i);
138+
if (!check(t) || stk.isEmpty() || !stk.pop().equals(t)) {
139+
return false;
140+
}
141+
} else if (code.startsWith("<", i)) {
142+
int j = i + 1;
143+
i = code.indexOf(">", j);
144+
if (i < 0) {
145+
return false;
146+
}
147+
String t = code.substring(j, i);
148+
if (!check(t)) {
149+
return false;
150+
}
151+
stk.push(t);
152+
}
153+
}
154+
return stk.isEmpty();
155+
}
156+
157+
private boolean check(String tag) {
158+
int n = tag.length();
159+
if (n < 1 || n > 9) {
160+
return false;
161+
}
162+
for (char c : tag.toCharArray()) {
163+
if (!Character.isUpperCase(c)) {
164+
return false;
165+
}
166+
}
167+
return true;
168+
}
169+
}
170+
```
81171

172+
### **C++**
173+
174+
```cpp
175+
class Solution {
176+
public:
177+
bool isValid(string code) {
178+
stack<string> stk;
179+
for (int i = 0; i < code.size(); ++i)
180+
{
181+
if (i && stk.empty()) return false;
182+
if (code.substr(i, 9) == "<![CDATA[")
183+
{
184+
i = code.find("]]>", i + 9);
185+
if (i < 0) return false;
186+
i += 2;
187+
}
188+
else if (code.substr(i, 2) == "</")
189+
{
190+
int j = i + 2;
191+
i = code.find('>', j);
192+
if (i < 0) return false;
193+
string t = code.substr(j, i - j);
194+
if (!check(t) || stk.empty() || stk.top() != t) return false;
195+
stk.pop();
196+
}
197+
else if (code.substr(i, 1) == "<")
198+
{
199+
int j = i + 1;
200+
i = code.find('>', j);
201+
if (i < 0) return false;
202+
string t = code.substr(j, i - j);
203+
if (!check(t)) return false;
204+
stk.push(t);
205+
}
206+
}
207+
return stk.empty();
208+
}
209+
210+
bool check(string tag) {
211+
int n = tag.size();
212+
if (n < 1 || n > 9) return false;
213+
for (char& c : tag)
214+
if (!isupper(c))
215+
return false;
216+
return true;
217+
}
218+
};
82219
```
83220

84221
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
public:
3+
bool isValid(string code) {
4+
stack<string> stk;
5+
for (int i = 0; i < code.size(); ++i)
6+
{
7+
if (i && stk.empty()) return false;
8+
if (code.substr(i, 9) == "<![CDATA[")
9+
{
10+
i = code.find("]]>", i + 9);
11+
if (i < 0) return false;
12+
i += 2;
13+
}
14+
else if (code.substr(i, 2) == "</")
15+
{
16+
int j = i + 2;
17+
i = code.find('>', j);
18+
if (i < 0) return false;
19+
string t = code.substr(j, i - j);
20+
if (!check(t) || stk.empty() || stk.top() != t) return false;
21+
stk.pop();
22+
}
23+
else if (code.substr(i, 1) == "<")
24+
{
25+
int j = i + 1;
26+
i = code.find('>', j);
27+
if (i < 0) return false;
28+
string t = code.substr(j, i - j);
29+
if (!check(t)) return false;
30+
stk.push(t);
31+
}
32+
}
33+
return stk.empty();
34+
}
35+
36+
bool check(string tag) {
37+
int n = tag.size();
38+
if (n < 1 || n > 9) return false;
39+
for (char& c : tag)
40+
if (!isupper(c))
41+
return false;
42+
return true;
43+
}
44+
};

0 commit comments

Comments
 (0)