leetcode

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

2023.cpp (475B)


      1 class Solution {
      2   public:
      3     int numOfPairs(const vector<string> &nums, const string target) {
      4         unordered_map<string, int> um;
      5         for (const auto &s : nums)
      6             um[s]++;
      7 
      8         int res = 0;
      9         for (int i = 0; i < target.size(); i++) {
     10             const string s1 = target.substr(0, i);
     11             const string s2 = target.substr(i);
     12             res += (s1 != s2) ? um[s1] * um[s2] : um[s1] * (um[s1] - 1);
     13         }
     14         return res;
     15     }
     16 };