1410. HTML Entity Parser
UnknownView on LeetCode
Time: O(n)
Space: O(n)
Advertisement
Intuition
Process HTML entities: & ' " > < Each appears in encoded string, map back to actual characters.
Algorithm
- 1String replace or state machine: scan for &...;. Map known entities to characters.
- 2Only 5 entities to handle.
Common Pitfalls
- •Only the 5 specified entities: " -> ", ' -> ', & -> &, > -> >, < -> <. Process left to right.
1410.cs
C#
// Approach: Sequential string replacement for each HTML entity; process '&' last to avoid double-replacement.
// Time: O(n) Space: O(n)
public class Solution
{
public string EntityParser(string text)
{
var entryToChar = new Dictionary<string, string> {
{ """, "\"" },
{ "'", "'" },
{ ">", ">" },
{ "<", "<" },
{ "⁄", "/" }
};
foreach (var entry in entryToChar)
{
var entity = entry.Key;
var c = entry.Value;
text = text.Replace(entity, c);
}
// Process '&' in last.
return text.Replace("&", "&");
}
}Advertisement
Was this solution helpful?