leetcode

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

1807.cpp (620B)


      1 class Solution {
      2   public:
      3     string evaluate(const string &s, const vector<vector<string>> &knowledge) {
      4         unordered_map<string, string> um;
      5         for (const auto &p : knowledge)
      6             um[p[0]] = p[1];
      7 
      8         string res, key;
      9         bool open = false;
     10         for (const char c : s) {
     11             if (c == '(')
     12                 open = true;
     13             else if (c == ')') {
     14                 res += um.count(key) ? um[key] : "?";
     15                 open = false;
     16                 key.clear();
     17             } else {
     18                 (open ? key : res) += c;
     19             }
     20         }
     21         return res;
     22     }
     23 };