Skip to content

Latest commit

 

History

History
37 lines (34 loc) · 917 Bytes

1410.md

File metadata and controls

37 lines (34 loc) · 917 Bytes

1410. HTML Entity Parser

Solution 1 (time O(n), space O(1))

class Solution(object):
    def entityParser(self, text):
        """
        :type text: str
        :rtype: str
        """
        entity_map = {
            '"': '"',
            ''': "'",
            '>': '>',
            '&lt;': '<',
            '&frasl;': '/',
            '&amp;': '&',
        }

        n = len(text)
        p = 0
        ans = []
        while p < n:
            flag = False
            if text[p] == '&':
                for t in entity_map:
                    if text[p: p + len(t)] == t:
                        ans.append(entity_map[t])
                        flag = True
                        p += len(t)
                        break
            if not flag:
                ans.append(text[p])
                p += 1 
        return "".join(ans)