summary of 4 solutions
solution 1\uFF1Aregex / python standard library\npython\ndef entityParser(self, text: str) -> str:        \n        d = { \'&quot;\':\'\\"\', \'&apos;\':"\'", \
## solution 1\uFF1Aregex / python standard library\n```python\ndef entityParser(self, text: str) -> str:        \n        d = { \'&quot;\':\'\\"\', \'&apos;\':"\'", \'&amp;\':\'&\', \'&gt;\':\'>\', \'&lt;\':\'<\', \'&frasl;\':\'/\' }        \n        return re.sub( "(&quot;|&apos;|&amp;|&gt;|&lt;|&frasl;)", lambda m: d[m.group(1)], text )\n```\n\n```python\ndef entityParser(self, text: str) -> str:\n        d = [(\'&quot;\', \'\\"\'),  (\'&apos;\', \'\\\'\'), (\'&gt;\', \'>\'), (\'&lt;\', \'<\'), (\'&frasl;\', \'/\'),(\'&amp;\', \'&\')]          \n        for pat, repl in d:\n            text = re.sub(pat, repl, text)\n        return text\n```\n\n```python\ndef entityParser(self, text: str) -> str:\n        d = { "&quot;":"\\"", "&apos;": "\\\'", "&gt;":">", "&lt;":"<", "&frasl;":"/", "&amp;": "&" }\n        for k,v in d.items():\n            text = text.replace( k, v )\n        return text\n```\n\nNote that in the last two solutions, `"&amp;"` needs to be replaced at last\uFF01Otherwise `"&amp;gt;"` will erroneously output `">"` instead of `"&gt;"`!\n\n\n## solution 2\uFF1Acharacter-by-character, peek into future\n```python\ndef entityParser(self, text: str) -> str:\n        d = { \'&quot;\':\'\\"\', \'&apos;\':"\'", \'&amp;\':\'&\', \'&gt;\':\'>\', \'&lt;\':\'<\', \'&frasl;\':\'/\' }                \n        ans, index, N = [], 0, len(text)\n        while index < N:\n            flag = True\n            if text[index] == \'&\':\n                for k in d.keys():\n                    length = len(k)\n                    if index + length <= N and text[index : index + length] == k:\n                        ans.append( d[k] )\n                        index += length\n                        flag = False\n                        break\n                        \n            if flag:\n                ans.append( text[index] )\n                index += 1\n        return "".join(ans)\n```\n\n## solution 3\uFF1Acharacter-by-character, with state\n```python\ndef entityParser(self, text: str) -> str:\n        mapping = { "&quot;":"\\"", "&apos;": "\\\'", "&gt;":">", "&lt;":"<", "&frasl;":"/", "&amp;": "&" }\n        state, ans = False, []\n        \n        for ch in text:\n            if not state:\n                if ch == \'&\':\n                    state, key = True, [\'&\']\n                else:\n                    ans.append(ch)\n            else:\n                key.append(ch)\n                if ch == \';\':\n                    state, key = False, "".join(key)\n                    ans.append( mapping.get(key, key) )                    \n                    \n        if state: ans.append( "".join(key) )\n        return "".join(ans)             \n```\n\n## solution 4\uFF1A2 pointers\n```python\ndef entityParser(self, text: str) -> str:\n        d = { \'&quot;\':\'\\"\', \'&apos;\':"\'", \'&amp;\':\'&\', \'&gt;\':\'>\', \'&lt;\':\'<\', \'&frasl;\':\'/\' }                \n        text, N = list(text), len(text)\n        slow, fast, start = 0, 0, N\n        while fast < N:\n            text[slow] = text[fast]\n            if text[slow] == \'&\':\n                start = slow\n            if text[slow] == \';\' and start < N:\n                k = "".join( text[start : slow+1] )\n                text[start] = d.get(k, k)\n                slow, start = start, N\n            slow, fast = slow+1, fast+1\n        return "".join( text[ : slow] )\n```\n\n
[]