HTML Entity Parser LeetCode solution in Python
1class Solution:2def entityParser(self, text: str) -> str:3d = {4""": '"',5"'": "'",6"&": "&",7">": ">",8"<": "<",9"⁄": "/",10}1112ans = ""13i = 014while i < len(text):15bag = ""1617# condition if find & and next char is not & also and handdling index out of range for i + 118if i + 1 < len(text) and text[i] == "&" and text[i + 1] != "&":1920# create subtring for speacial char till ";"21for j in range(i, len(text)):22if text[j] == ";":23bag += text[j]24break25else:26bag += text[j]2728# if that not present in dict we added same as it is29if bag not in d:30ans += bag31else:32ans += d[bag]3334# increment by length of bag35i += len(bag)3637# otherwise increment by 138else:39ans += text[i]40i += 141return ans