leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1410.cpp (984B)


      1 class Solution {
      2   public:
      3     string entityParser(const string &text) const {
      4         unordered_map<string, char> um = {{"&quot;", '"'}, {"&apos;", '\''}, {"&amp;", '&'},
      5                                           {"&gt;", '>'},   {"&lt;", '<'},    {"&frasl;", '/'}};
      6 
      7         string res, crnt;
      8         for (const char c : text) {
      9             if (crnt.empty()) {
     10                 if (c != '&')
     11                     res += c;
     12                 else
     13                     crnt = "&";
     14             } else {
     15                 if (c == '&') {
     16                     res += crnt;
     17                     crnt = "&";
     18                 } else {
     19                     crnt += c;
     20                     if (c == ';') {
     21                         if (um.count(crnt))
     22                             res += um[crnt];
     23                         else
     24                             res += crnt;
     25                         crnt.clear();
     26                     }
     27                 }
     28             }
     29         }
     30 
     31         return res + crnt;
     32     }
     33 };