HTML Entity Parser LeetCode solution in Python

1
class Solution:
2
def entityParser(self, text: str) -> str:
3
d = {
4
""": '"',
5
"'": "'",
6
"&": "&",
7
">": ">",
8
"&lt;": "<",
9
"&frasl;": "/",
10
}
11

12
ans = ""
13
i = 0
14
while i < len(text):
15
bag = ""
16

17
# condition if find & and next char is not & also and handdling index out of range for i + 1
18
if i + 1 < len(text) and text[i] == "&" and text[i + 1] != "&":
19

20
# create subtring for speacial char till ";"
21
for j in range(i, len(text)):
22
if text[j] == ";":
23
bag += text[j]
24
break
25
else:
26
bag += text[j]
27

28
# if that not present in dict we added same as it is
29
if bag not in d:
30
ans += bag
31
else:
32
ans += d[bag]
33

34
# increment by length of bag
35
i += len(bag)
36

37
# otherwise increment by 1
38
else:
39
ans += text[i]
40
i += 1
41
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0