↓Skip to main content
  1. LeetCode/

LeetCode 1410: HTML Entity Parser

·2 mins· ·
LeetCode Medium Hash-Table String
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-15

  • Submissions: Wrong Answer → Runtime Error ×2 → Accepted

Approach
#

Scan left to right, trying the six entities at each position with startswith (String Template 2):

  • On a match, output the character and jump i past the entity.
  • Otherwise output the original character and i += 1.

Output is never rescanned, so a replacement cannot combine with what follows into a new entity.

My Attempts
#

  1. Attempt 1 chained replace calls. ">" should become ">", but after & turns into &, it joins the following gt; into a new > that becomes >.
  2. Attempts 2 and 3 used two pointers to look ahead for ; without a bound check: with & at the end, text[right] went out of range, so "&" raised IndexError.
  3. Adding right < len(text) passed.

Solution
#

class Solution:
    def entityParser(self, text: str) -> str:
        table = {"&quot;": '"', "&apos;": "'", "&amp;": "&",
                 "&gt;": ">", "&lt;": "<", "&frasl;": "/"}
        out, i = [], 0
        while i < len(text):
            for pattern, ch in table.items():
                if text.startswith(pattern, i):
                    out.append(ch)
                    i += len(pattern)
                    break
            else:
                out.append(text[i])
                i += 1
        return "".join(out)

Complexity
#

Time O(n · E) with E the total entity length (a constant), space O(n).

Mistakes to Avoid
#

  • Do not chain replace; one replacement’s output is visible to the next.
  • Check bounds before looking ahead; startswith handles it for you.

Interview Explanation
#

I scan left to right and try to match the six entities at each position, outputting the character and skipping the entity on a match, or the original character otherwise. Output is never rescanned, so there is none of the chained-replace problem where a produced & forms a new entity. Time O(n).

Related

LeetCode 3121: Count the Number of Special Characters II
·2 mins
LeetCode Medium Hash-Table String
LeetCode 3121 note: track each letter’s last lowercase index and first uppercase index; it counts only when the former comes first
LeetCode 187: Repeated DNA Sequences
·1 min
LeetCode Medium Hash-Table String Bit-Manipulation Sliding-Window
LeetCode 187 note: count every length-10 substring in a hash map and return those seen more than once
LeetCode 208: Implement Trie (Prefix Tree)
·2 mins
LeetCode Medium Hash-Table String Design Trie
LeetCode 208 note: each node is a children dict plus an is_end flag; insert, search and startsWith all walk down character by character
LeetCode 424: Longest Repeating Character Replacement
·2 mins
LeetCode Medium Hash-Table String Sliding-Window
LeetCode 424 note: a variable sliding window where window length minus the top character count is the number of replacements; shrink when it exceeds k
LeetCode 981: Time Based Key-Value Store
·2 mins
LeetCode Medium Hash-Table String Binary-Search Design
LeetCode 981 note: store a (timestamp, value) list per key, with timestamps increasing; get binary searches the last timestamp <= the query
LeetCode 49: Group Anagrams
·2 mins
LeetCode Medium Array Hash-Table String Sorting
LeetCode 49 note: use each string’s sorted form as a key; strings sharing a key form one anagram group