✅✅Python Target Length with Dictionaries || Beats 100%✅✅
Approach\nFirst loop over nums and store the length of all prefixes of target in one dict (a) and all suffixes of target in another dict (b).\nThen go through d
# Approach\nFirst loop over nums and store the length of all prefixes of target in one dict (a) and all suffixes of target in another dict (b).\nThen go through dict a and check if the length of target minus the length of the suffixes is in dict b. If so then add the product of the number of prefixes times the number of suffixes with the sum of length target. Here it is important to check for the edge case that the length of target is exactly double the length of the current prefix and the suffix and prefix are equal. Here add up the product of numbers of itself times itself minus one to avoid counting doubles.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n\n# 100%\n![grafik.png](https://assets.leetcode.com/users/images/cb5889b3-9700-46cc-857f-16df1f7c1eaf_1714662566.8477125.png)\n\n# Code\n```\nclass Solution:\n    def numOfPairs(self, nums: List[str], target: str) -> int:      \n        l, r = len(target), 0\n        a, b = dict(), dict()\n        for i, n in enumerate(nums):\n            t = len(n)\n            if target.startswith(n):\n                if t in a: a[t] += 1\n                else: a[t] = 1\n            if target.endswith(n):\n                if t in b: b[t] += 1\n                else: b[t] = 1\n        for k, v in a.items():\n            if l/2==k and target[:k]==target[k:]: r += v*(v-1)\n            elif l-k in b: r += v*b[l-k]\n        return r\n```
['Python3']