Problem 1410

Abstract / Summary

Parse HTML entities in a string and replace them with their corresponding characters.

Note

The full problem description, including helpful examples, can be found on LeetCode.

Python 1410.py

class Solution:
    def entityParser(self, text: str) -> str:
        text = text.replace(""",  "\"")
        text = text.replace("'",  "'")
        # handle & at the end to avoid problems with single "&"
        text = text.replace(">",    ">")
        text = text.replace("&lt;",    "<")
        text = text.replace("&frasl;", "/")
        text = text.replace("&amp;",   "&")
        return text
10 lines | 425 bytes | submitted December 7, 2023 | GitHub   11% comment ratio | tab size 4 | low complexity

Difficulty

This problem is ranked as
easy medium hard

and has an acceptance rate of about 50% (50k+ submissions).

Typical patterns to solve this problem:
  • Hash Table
  • String
I solved 663 easy, 1250 medium and 309 hard problems

AI Disclaimer

Every single line of code was written by me, 100% from scratch, between August 2023 and August 2024.
However, two years later I set up this website and used various AI tools to generate most of the walkthrough / explanations below.

See this dedicated page for more details.
LeetCode profile | GitHub repo

Code Walkthrough

Overview

The provided Solution class implements a method entityParser that receives a string containing HTML-like entity references and returns a new string where those entities are replaced by their literal characters. The implementation follows a straightforward sequential replacement strategy, applying Python’s built-in str.replace method for each supported entity. By ordering the replacements carefully, the method ensures that overlapping patterns are resolved correctly, especially for the ampersand entity.

Key Steps

Normalizing Quote Entities

The first two replacement calls target the double-quote and single-quote entities.

text = text.replace("&quot;", "\"")
text = text.replace("&apos;", "'")

These statements scan the entire input and substitute every occurrence of &quot; with a literal double-quote (") and every occurrence of &apos; with a literal single-quote ('). This step handles the most common textual delimiters that may appear inside attribute values or content.

Replacing Comparison and Fraction Entities

The next group of replacements deals with greater-than, less-than, and fraction slash entities.

text = text.replace("&gt;",    ">")
text = text.replace("&lt;",    "<")
text = text.replace("&frasl;", "/")

Each call substitutes the corresponding HTML entity with its character counterpart. By processing these before the ampersand replacement, the method avoids inadvertently converting a partially decoded sequence that still contains an ampersand.

Final Ampersand Decoding

The last replacement handles the ampersand entity itself.

text = text.replace("&amp;",   "&")

Placing this step after all other replacements is intentional. If the ampersand were decoded earlier, a sequence such as &amp;gt; would become &gt; and then be incorrectly transformed into > in a later pass. By deferring the ampersand conversion, the method guarantees that only genuine &amp; tokens are turned into a literal &, while other entities that embed an ampersand remain untouched until they have already been processed.

Edge Cases Handled

The sequential replacement order resolves several subtle edge conditions.

When the input contains a literal ampersand that is not part of a recognized entity, such as &unknown;, the method leaves it unchanged because no replacement rule matches it. This behavior preserves unknown or malformed entities without raising errors.

If an entity appears multiple times or is nested within other text, each occurrence is replaced because str.replace operates globally on the string. For example, the string &lt;&gt;&amp; becomes <>& after all three relevant replacements are applied.

The method also correctly processes inputs where an entity appears at the very end of the string. Since each replace call scans the entire string, trailing entities are not missed.

Because the ampersand replacement is performed last, the code safely handles inputs that contain the literal sequence &amp;gt;. The first three replacements do not affect this pattern, and the final step converts &amp; to &, leaving &gt; untouched. The result is the original &gt; sequence, which can be interpreted later if needed.

Performance Optimizations

The implementation leverages Python’s native string replacement, which is implemented in C and therefore executes faster than a pure-Python loop that processes characters one by one. By chaining the replace calls, the code avoids creating intermediate data structures beyond the temporary strings produced by each call.

The ordering of replacements minimizes unnecessary work. Since the ampersand entity is the only one that could interfere with the others, handling it last prevents a scenario where an earlier replacement would generate new & characters that would then need to be scanned again. This reduces the total number of passes over the data to exactly five, one for each distinct entity, which is optimal given the fixed set of entities.

Because the method does not allocate additional containers such as lists or dictionaries, memory overhead remains low. Each replace call returns a new string, but the previous string becomes eligible for garbage collection immediately after the next assignment, keeping the peak memory usage proportional to the size of the input string plus a small constant factor.

Overall, the code achieves a balance between clarity and efficiency by using built-in operations that are highly optimized at the interpreter level.

written by openai gpt

ELI5 - Simplified Explanation

Imagine a box filled with secret code words. Sometimes, people use funny little nicknames to describe things on the computer, like using the letters and symbols & q u o t ; to mean a quote mark. This set of instructions is like a magic translator that looks through a whole story to find those nicknames. When it sees one of those special code words, it takes it out and swaps it for the real mark that we can actually read. It goes through the list one by one, changing the funny symbols into regular marks like slashes, arrows, and quotes so that everything looks exactly the way it should look when you read a book.

written by google gemini

Code Quality

Several LLMs ranked my code quality on a scale from 1 (very poor) to 10 (outstanding):
n = 26 | median = 7 | μ = 6.38 | σ = 1.52

Complexity Analysis

Time: linear
As a rule of thumb, the maximum input size for one second execution time is
  • factorial: up to 10 elements
  • exponential: up to 20 elements
  • polynomial: a few thousand elements
  • loglinear: up to 100,000 elements
  • linear: a few million elements
  • logarithmic: a billion elements
  • constant: practically unlimited
generated automatically

Other Solutions

I wrote a basic crawler to find other solutions to that problem.
Obviously, I did that after I solved the problem myself.

In general, AlgoMonster and WalkCCC solutions are high-quality and always worth a look.
I haven't checked most of the other solutions, but they might be helpful as well.

And of course, the LeetCode forum is always a good place (or even the best) to look for alternative approaches.

(click on a column header to sort the table)
author
author solutions
www AlgoMonster C++ Python Java TS
WalkCCC C++ Python Java
leetcode.ca C++ Python Java Go TS
Progiez C++ Python Java
github black-shadows C++
charles-wangkai Java
everthis JS
fsq C++
JoshCrozier JS
keineahnung2345 C++
lkwq007 Python
lzl124631x C++
varunu28 Java
YuriSpiridonov Python
AlgoMonster C++, Python, Java, TypeScript
WalkCCC C++, Python, Java
leetcode.ca C++, Go, Java, Python, TypeScript
Progiez C++, Java, Python
black-shadows C++, C++
charles-wangkai Java
everthis JavaScript
fsq C++
JoshCrozier JavaScript
keineahnung2345 C++
lkwq007 Python
lzl124631x C++
varunu28 Java
YuriSpiridonov Python
25 links | 14 authors | 6 languages

Videos

2 videos   did my crawler come up with invalid videos ? drop me a line.

Solved On The Same Day

6 solutions   December 7, 2023

Extra

There are a few extra pages giving you some insights into my setup:
  • Tech Stack - my development environment and tools
  • AI - how AI helped me create this website (NOT solving the problems)
  • Progress - progression over time
  • Quality - code quality