HTML Entity Parser - Problem

HTML entity parser is the parser that takes HTML code as input and replaces all the entities of the special characters by the characters themselves.

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 &lt; and symbol character is <
  • Slash: the entity is &frasl; and symbol character is /

Given an 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.

Input & Output

Example 1 — Basic Entities
$ Input: text = "&amp; is an HTML entity but &ambassador; is not."
Output: "& is an HTML entity but &ambassador; is not."
💡 Note: & is a valid entity that gets replaced with &, but &ambassador; is not a valid HTML entity so it stays unchanged
Example 2 — Multiple Entities
$ Input: text = "and I quote: &quot;...&quot;"
Output: "and I quote: \"...\""
💡 Note: " entities are replaced with quotation marks to form: and I quote: "..."
Example 3 — No Valid Entities
$ Input: text = "Stay home! Practice on Leetcode :)"
Output: "Stay home! Practice on Leetcode :)"
💡 Note: No HTML entities present, so the string remains unchanged

Constraints

  • 1 ≤ text.length ≤ 105
  • The string may contain any possible characters out of all the 256 ASCII characters.

Visualization

Tap to expand
HTML Entity Parser INPUT Input Text String: "&amp; is an HTML entity but &ambassador; is not." Entity Hash Map: &quot; --> " &apos; --> ' &amp; --> & &gt; --> > &lt; --> < &frasl; --> / 6 valid HTML entities stored in hash map ALGORITHM STEPS 1 Initialize Build hash map with entities 2 Scan for '&' Iterate through string 3 Extract Entity Find substring until ';' 4 Replace or Keep Lookup in map, replace if found Processing Example: &amp; --> found in map --> "&" &ambassador; --> NOT found --> keep as is Time: O(n) | Space: O(1) FINAL RESULT Output Text String: "& is an HTML entity but &ambassador; is not." Transformation Summary: &amp; --> & (1 replacement made) &ambassador; (kept unchanged - not valid) OK - Complete! Single pass parsing done Key Insight: Use a hash map for O(1) entity lookup. Scan once, when '&' is found, extract potential entity until ';'. Only replace if the exact entity exists in the map - this handles invalid entities like &ambassador; by keeping them unchanged. Single pass achieves O(n) time complexity. TutorialsPoint - HTML Entity Parser | Single Pass with Hash Map
Asked in
Amazon 15 Facebook 12 Google 8 Microsoft 6
28.0K Views
Medium Frequency
~15 min Avg. Time
890 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