C++ hash table solution with explanation | O(n) time complexity
Insert all the elements into hash table. (Unordered_map in c++)\n2. Traverse the array again. \n3. Check if this element can be the prefix of the target. If yes
1. Insert all the elements into hash table. (Unordered_map in c++)\n2. Traverse the array again. \n3. Check if this element can be the prefix of the target. If yes, find the string after this prefix in the target. Add its count to the answer.\n4. Edge case - if the current element and suffix matches, we have counted extra (since ```i!=j```).\n\nTime Complexity : ```O(n*m)```\nSpace Complexity : ```O(n*m)```\n\n```\nclass Solution {\npublic:\n    int numOfPairs(vector<string>& nums, string target) {\n        int cnt=0;\n        int n=target.length();\n        unordered_map<string, int> mp;\n        for(auto s:nums)\n            mp[s]++;\n        \n        for(int i=0; i<nums.size(); i++)\n        {\n            string temp=nums[i];\n            int len=temp.length();\n            if(len>n)\n            {\n                continue;\n            }\n            string s1=target.substr(len, n-len);\n            string s2=target.substr(0,len);\n           if(temp== s2)\n            {\n                cnt+=mp[s1];\n                if(s1==s2)\n                    cnt--;\n            }\n        }\n        return cnt;\n    }\n};\n```\nPlease upvote this solution :)
[]