Mutating counts without cleanup
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Move from brute-force thinking to an efficient approach using hash map strategy.
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.
The special characters and their entities for HTML are:
" and symbol character is ".' and symbol character is '.& and symbol character is &.> and symbol character is >.< and symbol character is <.⁄ and symbol character is /.Given the input text string to the HTML parser, you have to implement the entity parser.
Return the text after replacing the entities by the special characters.
Example 1:
Input: text = "& is an HTML entity but &ambassador; is not." Output: "& is an HTML entity but &ambassador; is not." Explanation: The parser will replace the & entity by &
Example 2:
Input: text = "and I quote: "..."" Output: "and I quote: \"...\""
Constraints:
1 <= text.length <= 105Problem summary: 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. The special characters and their entities for HTML are: Quotation Mark: the entity is " and symbol character is ". Single Quote Mark: the entity is ' and symbol character is '. Ampersand: the entity is & and symbol character is &. Greater Than Sign: the entity is > and symbol character is >. Less Than Sign: the entity is < and symbol character is <. Slash: the entity is ⁄ and symbol character is /. Given the input text string to the HTML parser, you have to implement the entity parser. Return the text after replacing the entities by the special characters.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Hash Map
"& is an HTML entity but &ambassador; is not."
"and I quote: "...""
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1410: HTML Entity Parser
class Solution {
public String entityParser(String text) {
Map<String, String> d = new HashMap<>();
d.put(""", "\"");
d.put("'", "'");
d.put("&", "&");
d.put(">", ">");
d.put("<", "<");
d.put("⁄", "/");
StringBuilder ans = new StringBuilder();
int i = 0;
int n = text.length();
while (i < n) {
boolean found = false;
for (int l = 1; l < 8; ++l) {
int j = i + l;
if (j <= n) {
String t = text.substring(i, j);
if (d.containsKey(t)) {
ans.append(d.get(t));
i = j;
found = true;
break;
}
}
}
if (!found) {
ans.append(text.charAt(i++));
}
}
return ans.toString();
}
}
// Accepted solution for LeetCode #1410: HTML Entity Parser
func entityParser(text string) string {
d := map[string]string{
""": "\"",
"'": "'",
"&": "&",
">": ">",
"<": "<",
"⁄": "/",
}
var ans strings.Builder
i, n := 0, len(text)
for i < n {
found := false
for l := 1; l < 8; l++ {
j := i + l
if j <= n {
t := text[i:j]
if val, ok := d[t]; ok {
ans.WriteString(val)
i = j
found = true
break
}
}
}
if !found {
ans.WriteByte(text[i])
i++
}
}
return ans.String()
}
# Accepted solution for LeetCode #1410: HTML Entity Parser
class Solution:
def entityParser(self, text: str) -> str:
d = {
'"': '"',
''': "'",
'&': "&",
">": '>',
"<": '<',
"⁄": '/',
}
i, n = 0, len(text)
ans = []
while i < n:
for l in range(1, 8):
j = i + l
if text[i:j] in d:
ans.append(d[text[i:j]])
i = j
break
else:
ans.append(text[i])
i += 1
return ''.join(ans)
// Accepted solution for LeetCode #1410: HTML Entity Parser
struct Solution;
impl Solution {
fn entity_parser(text: String) -> String {
text.replace(""", "\"")
.replace("'", "'")
.replace("⁄", "/")
.replace("<", "<")
.replace(">", ">")
.replace("&", "&")
}
}
#[test]
fn test() {
let text = "& is an HTML entity but &ambassador; is not.".to_string();
let res = "& is an HTML entity but &ambassador; is not.".to_string();
assert_eq!(Solution::entity_parser(text), res);
let text = "and I quote: "..."".to_string();
let res = "and I quote: \"...\"".to_string();
assert_eq!(Solution::entity_parser(text), res);
let text = "Stay home! Practice on Leetcode :)".to_string();
let res = "Stay home! Practice on Leetcode :)".to_string();
assert_eq!(Solution::entity_parser(text), res);
let text = "x > y && x < y is always false".to_string();
let res = "x > y && x < y is always false".to_string();
assert_eq!(Solution::entity_parser(text), res);
let text = "leetcode.com⁄problemset⁄all".to_string();
let res = "leetcode.com/problemset/all".to_string();
assert_eq!(Solution::entity_parser(text), res);
}
// Accepted solution for LeetCode #1410: HTML Entity Parser
function entityParser(text: string): string {
const d: Record<string, string> = {
'"': '"',
''': "'",
'&': '&',
'>': '>',
'<': '<',
'⁄': '/',
};
let ans: string = '';
let i: number = 0;
const n: number = text.length;
while (i < n) {
let found: boolean = false;
for (let l: number = 1; l < 8; ++l) {
const j: number = i + l;
if (j <= n) {
const t: string = text.substring(i, j);
if (d.hasOwnProperty(t)) {
ans += d[t];
i = j;
found = true;
break;
}
}
}
if (!found) {
ans += text[i++];
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each element, scan the rest of the array looking for a match. Two nested loops give n × (n−1)/2 comparisons = O(n²). No extra space since we only use loop indices.
One pass through the input, performing O(1) hash map lookups and insertions at each step. The hash map may store up to n entries in the worst case. This is the classic space-for-time tradeoff: O(n) extra memory eliminates an inner loop.
Review these before coding to avoid predictable interview regressions.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.