HTML Entity Parser

medium hash map string

Problem

Replace HTML entities in the input string by their characters: &quot;", &apos;', &amp;&, &gt;>, &lt;<, &frasl;/.

Inputtext = "&amp; is an HTML entity but &ambassador; is not."
Output"& is an HTML entity but &ambassador; is not."
Only the registered entity names are replaced.

def entity_parser(text):
    table = {"&quot;": '"', "&apos;": "'", "&amp;": "&",
             "&gt;": ">", "&lt;": "<", "&frasl;": "/"}
    out, i, n = [], 0, len(text)
    while i < n:
        if text[i] == "&":
            matched = False
            for ent, ch in table.items():
                if text.startswith(ent, i):
                    out.append(ch); i += len(ent); matched = True; break
            if not matched:
                out.append("&"); i += 1
        else:
            out.append(text[i]); i += 1
    return "".join(out)
function entityParser(text) {
  const table = { "&quot;": '"', "&apos;": "'", "&amp;": "&",
                  "&gt;": ">", "&lt;": "<", "&frasl;": "/" };
  let out = "", i = 0;
  while (i < text.length) {
    if (text[i] === "&") {
      let matched = false;
      for (const ent in table) {
        if (text.startsWith(ent, i)) {
          out += table[ent]; i += ent.length; matched = true; break;
        }
      }
      if (!matched) { out += "&"; i++; }
    } else { out += text[i++]; }
  }
  return out;
}
class Solution {
    public String entityParser(String text) {
        Map<String, String> table = new LinkedHashMap<>();
        table.put("&quot;", "\""); table.put("&apos;", "'");
        table.put("&amp;", "&"); table.put("&gt;", ">");
        table.put("&lt;", "<"); table.put("&frasl;", "/");
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (i < text.length()) {
            if (text.charAt(i) == '&') {
                boolean matched = false;
                for (var e : table.entrySet()) {
                    if (text.startsWith(e.getKey(), i)) {
                        sb.append(e.getValue()); i += e.getKey().length(); matched = true; break;
                    }
                }
                if (!matched) { sb.append('&'); i++; }
            } else { sb.append(text.charAt(i++)); }
        }
        return sb.toString();
    }
}
string entityParser(string text) {
    vector<pair<string,string>> table = {
        {"&quot;", "\""}, {"&apos;", "'"}, {"&amp;", "&"},
        {"&gt;", ">"}, {"&lt;", "<"}, {"&frasl;", "/"}
    };
    string out;
    int i = 0, n = text.size();
    while (i < n) {
        if (text[i] == '&') {
            bool matched = false;
            for (auto& [ent, ch] : table) {
                if (text.compare(i, ent.size(), ent) == 0) {
                    out += ch; i += ent.size(); matched = true; break;
                }
            }
            if (!matched) { out += '&'; i++; }
        } else { out += text[i++]; }
    }
    return out;
}
Time: O(n) Space: O(n)