C++ Simple and Easy Solution With Detailed Explanation
Idea:\nFirst, we store in a map the frequencies of the strings, so that we can find easily which strings we have and how many.\nNow, we iterate through the freq
**Idea:**\nFirst, we store in a map the frequencies of the strings, so that we can find easily which strings we have and how many.\nNow, we iterate through the `freq` map.\nFor every string:\n1. We check if it\'s a prefix of our `target`.\n2. If yes, first case is that the target is exactly twice the prefix. If so, we add `frq*(frq-1)` to `res`.\nThe reason is that the number of combinations for a pattern with frequency n is n * (n-1).\n3. Otherwise we look in the map if we have the suffix, if so - we add the product of their frequencies to `res`.\n\n**Time Complexity:** O(n)\n**Space Complexity:** O(n)\n```\nclass Solution {\npublic:\n    int numOfPairs(vector<string>& nums, string target) {\n        unordered_map<string, int> freq;\n        for (auto num : nums) if (num.size() < target.size()) freq[num]++;\n        \n        int res = 0;\n        for (auto [s, frq] : freq) {\n            \n            if (target.find(s) == 0) {\n                \n                if (s + s == target) \n                    res += frq*(frq-1);\n                \n                else \n                    res += frq * freq[target.substr(s.size())];\n            }\n        }\n        \n        return res;\n    }\n};\n```\n**Like it? please upvote!**
['C']