⭐ Brute Force + Hashing - Time Complexity + Space Complexity + Explanation ⭐
Brute Force\n\nTime Complexity - The two nested loops create create a time complexity of O(N^2).\nSpace Complexity - Since we are not using any extra space, spa
# **Brute Force**\n\nTime Complexity - The two nested loops create create a time complexity of O(N^2).\nSpace Complexity - Since we are not using any extra space, space complexity is O(1).\nExplanation - We take each and every pair possible using nested loops and concatenate the pair to check if we have achieved the target.\n\n```\nclass Solution {\npublic:\n    int numOfPairs(vector<string>& nums, string target) {\n        int n = nums.size();\n        int count = 0;\n        for(int i = 0; i < n; i++){\n            for(int j = 0; j < n; j++){\n                if(i != j && nums[i] + nums[j] == target) count++;\n            }\n        }\n        return count;\n    }\n};\n```\n\n# **Hashing Approach (optimized)**\nTime Complexity - Iterating over the entire array takes O(N) time.\nSpace Complexity - Since we are using a hashmap, space complexity is O(N).\nExplanation - We store all the possible suffixes in a hashmap mp. Iterating over the array, we take each word individually and check if they can be a possible prefix. Once we find a possible prefix, we find the suffix required to be concatenated with it in order to create our target. If we find that this suffix is present in our hashmap mp, we increase the value of count by mp[suffix] (if prefix and suffix are equal, we increase count by mp[suffix] - 1 to avoid taking the same string as prefix and suffix).\n\n```\nclass Solution {\npublic:\n    int numOfPairs(vector<string>& nums, string target) {\n        int n = nums.size();\n        int prefixLength, targetLength = target.length(), count = 0;\n        unordered_map<string, int> mp;\n        for(auto suffix : nums) mp[suffix]++;\n        \n        for(auto prefix : nums){\n            prefixLength = prefix.length();\n            if(target.substr(0, prefixLength) == prefix){\n                string suffix = target.substr(prefixLength);\n                if(mp.find(suffix) != mp.end()){\n                    count += (prefix == suffix) ? (mp[suffix] - 1) : (mp[suffix]); \n                }\n            }\n        }\n        return count;\n    }\n};\n```\n\nKindly upvote if you found the solution helpful :)\n**For more such explanations of LeetCode problems along with their C++ solutions:**\nhttps://github.com/Arya-Gupta/LeetCode
['String', 'C', 'C++']