Java | Prefix/Suffix Counts | HashMap
\nTo understand the code better,\ndry run for the example test case\nnums = ["777","7","77","77"], target = "7777"\n\nMethod 1 : Brute Force\n\nclass Solution {
```\nTo understand the code better,\ndry run for the example test case\nnums = ["777","7","77","77"], target = "7777"\n```\nMethod 1 : Brute Force\n```\nclass Solution {\n    public int numOfPairs(String[] nums, String target) {\n        int result = 0 ;\n        int n = nums.length ;\n        \n        for (int i = 0; i < n; i++)\n            for (int j = i+1; j < n; j++) {\n                if ((nums[i] + nums[j]).equals(target))\n                    result++ ;\n                if ((nums[j] + nums[i]).equals(target))\n                    result++ ;\n            }\n        \n        return result ;\n    }\n}\n```\nMethod 2 : Using HashMap (Optimized Solution)\n```\nclass Solution {\n    public int numOfPairs(String[] nums, String target) {\n        HashMap<String, Integer> map = new HashMap<>() ;\n        int N = nums.length ;\n        int n = target.length() ;\n        int result = 0 ;\n        \n        for (int i = 0; i < N; i++) {\n            int m = nums[i].length() ;\n            \n            if (target.startsWith(nums[i])) {\n                String suffix = target.substring(m) ;\n                if (map.containsKey(suffix))\n                    result += map.get(suffix) ;\n            }\n            \n            if (target.endsWith(nums[i])) {\n                String prefix = target.substring(0, n-m) ;\n                if (map.containsKey(prefix))\n                    result += map.get(prefix) ;\n            }\n            \n            map.put(nums[i], map.getOrDefault(nums[i], 0)+1) ;\n        }\n        return result ;\n    }\n}\n```
['Java']