C++ Frequency Map
See my latest update in repo LeetCode\n\n## Solution 1. Prefix State Map\n\ncpp\n// OJ: https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenat
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Prefix State Map\n\n```cpp\n// OJ: https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/\n// Author: github.com/lzl124631x\n// Time: O(NW) where `N` is the length of `A` and `W` is the maximum length of elements in `A`\n// Space: O(U) where `U` is the number of unique strings in `A`.\nclass Solution {\npublic:\n    int numOfPairs(vector<string>& A, string s) {\n        unordered_map<string, int> m;\n        int ans = 0;\n        for (auto &t : A) {\n            if (t.size() > s.size()) continue;\n            if (s.substr(0, t.size()) == t) ans += m[s.substr(t.size())];\n            m[t]++;\n        }\n        m.clear();\n        reverse(begin(A), end(A));\n        for (auto &t : A) {\n            if (t.size() > s.size()) continue;\n            if (s.substr(0, t.size()) == t) ans += m[s.substr(t.size())];\n            m[t]++;\n        }\n        return ans;\n    }\n};\n```\n\n## Solution 2. Frequency Map\n\n```cpp\n// OJ: https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/\n// Author: github.com/lzl124631x\n// Time: O(NW) where `N` is the length of `A` and `W` is the maximum length of elements in `A`\n// Space: O(U) where `U` is the number of unique strings in `A`.\nclass Solution {\npublic:\n    int numOfPairs(vector<string>& A, string s) {\n        unordered_map<string, int> m;\n        for (auto &t : A) m[t]++;\n        int ans = 0;\n        for (auto &[prefix, cnt] : m) {\n            if (prefix.size() > s.size()) continue;\n            if (s.substr(0, prefix.size()) == prefix) {\n                auto suffix = s.substr(prefix.size());\n                ans += m[prefix] * m[suffix];\n                if (prefix == suffix) ans -= m[prefix];\n            }\n        }\n        return ans;\n    }\n};\n```
[]