HTML Entity Parser - Problem
Imagine you're building a web browser or text editor that needs to display HTML content correctly! HTML Entity Parser is a fundamental text processing problem where you need to convert HTML entities back to their corresponding special characters.

In HTML, certain characters like <, >, &, and quotes have special meanings, so they're encoded as entities to avoid conflicts. Your job is to decode these entities back to readable characters.

The Entity Mappings:
&quot;" (Quotation Mark)
&apos;' (Single Quote)
&amp;& (Ampersand)
&gt;> (Greater Than)
&lt;< (Less Than)
&frasl;/ (Slash)

Goal: Transform an HTML string by replacing all entities with their actual characters, returning clean, readable text.

Input & Output

example_1.py — Basic HTML Entities
$ Input: text = "&amp; is an HTML entity but &ambassador; is not."
Output: "& is an HTML entity but &ambassador; is not."
💡 Note: The & entity is converted to & character, while &ambassador; is not a valid entity so it remains unchanged.
example_2.py — Multiple Entities
$ Input: text = "and I quote: &quot;...&quot;"
Output: "and I quote: \"...\""
💡 Note: Both " entities are converted to quotation marks, resulting in proper quote formatting.
example_3.py — Complex Expression
$ Input: text = "Stay home! Practice on Leetcode :)"
Output: "Stay home! Practice on Leetcode :)"
💡 Note: No entities present in the string, so it returns unchanged. This tests the edge case of no modifications needed.

Constraints

  • 1 ≤ text.length ≤ 105
  • The string may contain any possible characters out of all the 256 ASCII characters
  • Only the listed entities need to be replaced - ignore any other & patterns
  • Entities are case-sensitive and must match exactly

Visualization

Tap to expand
HTML Entity Parser VisualizationInput: "Hello &gt; World"Scanning Process:Current PositionEntity Map&quot; → "&amp; → &&gt; → >&lt; → <O(1) lookupProcessing Steps1. Find '&' at position 72. Check: '&gt;' matches entity3. Replace with '>' and skip 4 chars4. Continue scanning from next positionResult: "Hello > World"Time Complexity: O(n) | Space Complexity: O(n)Single pass with efficient hash table lookups
Understanding the Visualization
1
Scanner Initialization
Start with a hash map of entity codes and begin scanning the input text
2
Character Processing
Move through each character, building the result string as we go
3
Entity Detection
When we find '&', check if it starts a valid entity pattern
4
Smart Replacement
Replace valid entities with their characters, or keep original text if no match
Key Takeaway
🎯 Key Insight: By using a hash table and processing character by character, we achieve optimal O(n) time complexity while handling all entity patterns correctly in a single pass.
Asked in
Amazon 15 Microsoft 12 Google 8 Meta 6
24.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen