-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhTMLEntityParser.cpp
More file actions
28 lines (27 loc) · 885 Bytes
/
hTMLEntityParser.cpp
File metadata and controls
28 lines (27 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Source: https://leetcode.com/problems/html-entity-parser/
// Author: Miao Zhang
// Date: 2021-04-30
class Solution {
public:
string entityParser(string text) {
map<string, string> m{{""", "\""}, {"'", "'"},
{"&", "&"}, {">", ">"},
{"<", "<"}, {"⁄", "/"}};
string res;
string buf;
for (char& c: text) {
buf += c;
if (buf.back() != ';') continue;
int buflen = buf.size();
for (const auto& [k, v]: m) {
int klen = k.length();
if (buflen >= klen && buf.substr(buflen - klen) == k) {
res += buf.substr(0, buflen - klen) + v;
buf.clear();
break;
}
}
}
return res + buf;
}
};