Skip to content

Commit 0c8eefc

Browse files
refactor 591
1 parent 806d87c commit 0c8eefc

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

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

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -100,54 +100,59 @@ For simplicity, you could assume the input code (including the any characters me
100100
only contain letters, digits, '<','>','/','!','[',']' and ' '.
101101
*/
102102
public class _591 {
103-
104-
/**Credit: https://discuss.leetcode.com/topic/91300/java-solution-use-startswith-and-indexof*/
105-
public boolean isValid(String code) {
106-
Deque<String> stack = new ArrayDeque<>();
107-
for (int i = 0; i < code.length(); ) {
108-
if (i > 0 && stack.isEmpty()) {
109-
return false;
110-
}
111-
if (code.startsWith("<![CDATA[", i)) {
112-
int j = i + 9;//"<![CDATA[" length is 9
113-
i = code.indexOf("]]>", j);
114-
if (i < 0) {
115-
return false;
116-
}
117-
i += 3;//"]]>" length is 3
118-
} else if (code.startsWith("</", i)) {
119-
int j = i + 2;
120-
i = code.indexOf(">", j);
121-
if (i < 0 || i == j || i - j > 9) {
103+
104+
public static class Solution1 {
105+
106+
/**
107+
* Credit: https://discuss.leetcode.com/topic/91300/java-solution-use-startswith-and-indexof
108+
*/
109+
public boolean isValid(String code) {
110+
Deque<String> stack = new ArrayDeque<>();
111+
for (int i = 0; i < code.length(); ) {
112+
if (i > 0 && stack.isEmpty()) {
122113
return false;
123114
}
124-
for (int k = j; k < i; k++) {
125-
if (!Character.isUpperCase(code.charAt(k))) {
115+
if (code.startsWith("<![CDATA[", i)) {
116+
int j = i + 9;//"<![CDATA[" length is 9
117+
i = code.indexOf("]]>", j);
118+
if (i < 0) {
126119
return false;
127120
}
128-
}
129-
String s = code.substring(j, i++);
130-
if (stack.isEmpty() || !stack.pop().equals(s)) {
131-
return false;
132-
}
133-
} else if (code.startsWith("<", i)) {
134-
int j = i + 1;
135-
i = code.indexOf(">", j);
136-
if (i < 0 || i == j || i - j > 9) {
137-
return false;
138-
}
139-
for (int k = j; k < i; k++) {
140-
if (!Character.isUpperCase(code.charAt(k))) {
121+
i += 3;//"]]>" length is 3
122+
} else if (code.startsWith("</", i)) {
123+
int j = i + 2;
124+
i = code.indexOf(">", j);
125+
if (i < 0 || i == j || i - j > 9) {
126+
return false;
127+
}
128+
for (int k = j; k < i; k++) {
129+
if (!Character.isUpperCase(code.charAt(k))) {
130+
return false;
131+
}
132+
}
133+
String s = code.substring(j, i++);
134+
if (stack.isEmpty() || !stack.pop().equals(s)) {
141135
return false;
142136
}
137+
} else if (code.startsWith("<", i)) {
138+
int j = i + 1;
139+
i = code.indexOf(">", j);
140+
if (i < 0 || i == j || i - j > 9) {
141+
return false;
142+
}
143+
for (int k = j; k < i; k++) {
144+
if (!Character.isUpperCase(code.charAt(k))) {
145+
return false;
146+
}
147+
}
148+
String s = code.substring(j, i++);
149+
stack.push(s);
150+
} else {
151+
i++;
143152
}
144-
String s = code.substring(j, i++);
145-
stack.push(s);
146-
} else {
147-
i++;
148153
}
154+
return stack.isEmpty();
149155
}
150-
return stack.isEmpty();
151156
}
152157

153158
}

src/test/java/com/fishercoder/_591Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import static org.junit.Assert.assertEquals;
88

99
public class _591Test {
10-
private static _591 test;
10+
private static _591.Solution1 test;
1111

1212
@BeforeClass
1313
public static void setup() {
14-
test = new _591();
14+
test = new _591.Solution1();
1515
}
1616

1717
@Test

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy