Skip to content

Commit 38eed1e

Browse files
add 1410
1 parent c8bd614 commit 38eed1e

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1410|[HTML Entity Parser](https://leetcode.com/problems/html-entity-parser/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1410.java) | |Medium|String, Stack|
1112
|1409|[Queries on a Permutation With Key](https://leetcode.com/problems/queries-on-a-permutation-with-key/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1409.java) | |Medium|Array|
1213
|1408|[String Matching in an Array](https://leetcode.com/problems/string-matching-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1408.java) | |Easy|String|
1314
|1403|[Minimum Subsequence in Non-Increasing Order](https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1403.java) | |Easy|Greedy, Sort|
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1410. HTML Entity Parser
5+
*
6+
* HTML entity parser is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself.
7+
* The special characters and their entities for HTML are:
8+
* Quotation Mark: the entity is " and symbol character is ".
9+
* Single Quote Mark: the entity is ' and symbol character is '.
10+
* Ampersand: the entity is & and symbol character is &.
11+
* Greater Than Sign: the entity is > and symbol character is >.
12+
* Less Than Sign: the entity is &lt; and symbol character is <.
13+
* Slash: the entity is &frasl; and symbol character is /.
14+
* Given the input text string to the HTML parser, you have to implement the entity parser.
15+
*
16+
* Return the text after replacing the entities by the special characters.
17+
*
18+
* Example 1:
19+
* Input: text = "&amp; is an HTML entity but &ambassador; is not."
20+
* Output: "& is an HTML entity but &ambassador; is not."
21+
* Explanation: The parser will replace the &amp; entity by &
22+
*
23+
* Example 2:
24+
* Input: text = "and I quote: &quot;...&quot;"
25+
* Output: "and I quote: \"...\""
26+
*
27+
* Example 3:
28+
* Input: text = "Stay home! Practice on Leetcode :)"
29+
* Output: "Stay home! Practice on Leetcode :)"
30+
*
31+
* Example 4:
32+
* Input: text = "x &gt; y &amp;&amp; x &lt; y is always false"
33+
* Output: "x > y && x < y is always false"
34+
*
35+
* Example 5:
36+
* Input: text = "leetcode.com&frasl;problemset&frasl;all"
37+
* Output: "leetcode.com/problemset/all"
38+
*
39+
* Constraints:
40+
* 1 <= text.length <= 10^5
41+
* The string may contain any possible characters out of all the 256 ASCII characters.
42+
* */
43+
public class _1410 {
44+
public static class Solution1 {
45+
public String entityParser(String text) {
46+
StringBuilder sb = new StringBuilder();
47+
for (int i = 0; i < text.length(); i++) {
48+
if (text.charAt(i) != '&') {
49+
sb.append(text.charAt(i));
50+
} else {
51+
if (i + 7 <= text.length() && text.substring(i, i + 7).equals("&frasl;")) {
52+
sb.append("/");
53+
i += 6;
54+
} else if (i + 6 <= text.length() && text.substring(i, i + 6).equals("&quot;")) {
55+
sb.append("\"");
56+
i += 5;
57+
} else if (i + 6 <= text.length() && text.substring(i, i + 6).equals("&apos;")) {
58+
sb.append("'");
59+
i += 5;
60+
} else if (i + 5 <= text.length() && text.substring(i, i + 5).equals("&amp;")) {
61+
sb.append("&");
62+
i += 4;
63+
} else if (i + 4 <= text.length() && text.substring(i, i + 4).equals("&gt;")) {
64+
sb.append(">");
65+
i += 3;
66+
} else if (i + 4 <= text.length() && text.substring(i, i + 4).equals("&lt;")) {
67+
sb.append("<");
68+
i += 3;
69+
} else {
70+
sb.append(text.charAt(i));
71+
}
72+
}
73+
}
74+
return sb.toString();
75+
}
76+
}
77+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1410;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1410Test {
10+
private static _1410.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1410.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("& is an HTML entity but &ambassador; is not.", solution1.entityParser("&amp; is an HTML entity but &ambassador; is not."));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)