Beats 97.27% || JAVA || HashMap || O(N)
Intuition\n Describe your first thoughts on how to solve this problem. \nUse Hash Map ( solution somewhat similar to 2Sum problem)\n\nTried my best to explain ,
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nUse Hash Map ( solution somewhat similar to 2Sum problem)\n\nTried my best to explain , hope this explaination was not in vain \n\ncase to be considered for better understanding  ->\n["74","1","67","1","74"] & target "174"\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Store all the elements of nums in a map along with their frequencies\n- now start making the substring of the target and check whether both the substrings are present in the map or not\n- if both present increase counter\n- the edge case would be where we have both the s1 & s2 are same and equal to target , here we see that the no\'s of pairs are n*(n-1) , where n is the frequency.\n- another case i found where this failed was s1 & s2 were not same but had more than 1 frequency , so instead of increasing the counter by 1 , what we did was found the pairs by multiplying the freq (case is given above).\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n    O(n)\n\n![image.png](https://assets.leetcode.com/users/images/f8364583-f3a3-4a27-a53c-701ca55eeed0_1713549293.9749033.png)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(n)\n\n# Code\n```\nclass Solution {\n    public int numOfPairs(String[] nums, String target) {\n        HashMap<String, Integer> map = new HashMap<>();\n        for( int i = 0; i < nums.length ; i++)\n        {\n            int freq = map.getOrDefault( nums[i] , 0)+1;\n            map.put( nums[i] , freq );\n        }\n\n        int count = 0;\n        int n = target.length();\n        for( int i = 0; i < n-1 ; i++)\n        {\n            String s1 = target.substring(0,i+1);\n            String s2 = target.substring(i+1,n);\n            if( map.containsKey(s1) && map.containsKey(s2) && !s1.equals(s2) )\n            {\n                int freq1 = map.get(s1);\n                int freq2 = map.get(s2);\n                count += freq1*freq2;\n            }\n            if( s1.equals(s2) && map.containsKey(s1) )\n            {\n                int freq = map.get(s1);\n                count += freq*(freq-1);\n                map.remove(s1);\n            }\n        }\n        return count;\n    }\n}\n```
['Hash Table', 'Java']