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
The Entity Mappings:
•
•
•
•
•
•
Goal: Transform an HTML string by replacing all entities with their actual characters, returning clean, readable text.
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:
•
" → " (Quotation Mark)•
' → ' (Single Quote)•
& → & (Ampersand)•
> → > (Greater Than)•
< → < (Less Than)•
⁄ → / (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 = "& 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: "...""
›
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code