O(1) space, O(n*k) Time || Hashmap approach [Python3]
Bear with me, answer isn\'t hard at all\njust follow the intuition\n> Please Leave a like if you liked the approach\n\n# Intuition\n Describe your first thought
Bear with me, answer isn\'t hard at all\njust follow the intuition\n> Please Leave a like if you liked the approach\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nAnalyzing the input array `words` and the problem description `Two strings are similar if they consist of the same characters.` we can see that words can be broken down into their unique characters. For example: *abba* is just \'ab\' and *acab* is just \'acb\'\n\nwith that, I realized that we could store all of the unique chars as keys for a hash and the corresponding values would be the **counts of similar strings**\n{ KEY (unique value): VALUE (**count of similar strings**) }\n\nFor example \n`Example 1: Input: words = ["aba","aabb","abcd","bac","aabc"]`\nwould look something like this:\n![image.png](https://assets.leetcode.com/users/images/b002a5be-55dc-44bc-8405-549d499529ed_1675999877.4854085.png)\n\nNow that we know how many words share the same letters we can find out how many unique pairs there are by using the n(n-1) /2 formula which finds the non-repeating pairs (idk the name of that formula, and YOU\'RE NOT EXPECTED TO KNOW THAT FORMULA).\n\n *Wondering that if I know the number of elements, how would I know how many unique pairs we can form -> I googled that and found that formula.*\n\nanyway, now that we know that formula and how many instances there are for similar strings, we can iterate through every similar string key in the map, apply that formula to the value and add the result to a counter\n![image.png](https://assets.leetcode.com/users/images/0c616454-80c8-43c9-9675-d8f6465e0edb_1676001084.1398206.png)\nlastly, we return that counter\n\n\n# How to convert the word to a key\nTo convert the word to a key we do this in order:\n1. convert string into a set (time complexity of this operation will be K: the average length of word[i])\n`This has a flaw,\nif words are in order like \'abba\' it will work ok since it will store it as \'ab\' but if words are not in order like "bac" it will store it out of order, and it will not match with it\'s corresponding \'abc\' key`\n2. To "order" the characters we will sort the set (time complexity will be O(26) at most, since the set made sure we only have unique characters and we know the string only contains lowercase characters)\n3. Finally we convert this sorted list into a string by using the ".join() operator. This is also constant time since there are max 26 characters.\n\n# Complexity\n- Time complexity: O(n * k) where n is the size of `words` and k is the average length of `words[i]`\n    - we traverse the whole `words` array : O(n)  \n        - inside that traversal, we create create a set for every word: O(k)\n        - we sort that set: O(26) -> O(1)\n        - we `"".join()` to create sorted array into a string: O(26) -> O(1)\n\n    - we traverse the hashmap: O(26) -> O(1)\n\n- Space complexity: O(1)\n    - we create a set but it will have at most (26) characters\n  - we create a hash, but since the keys will only be unique chars we know that this takes constant space (see image)\n![image.png](https://assets.leetcode.com/users/images/9d866712-185f-4021-a8b9-36dd85e0132e_1676002074.0238433.png)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n> Please Leave a like if you liked the approach\n\n# Code\n``` python []\nclass Solution:\n    def similarPairs(self, words: List[str]) -> int:\n        wordMap = {}\n\n        #we traverse the input array\n        for word in words:\n            #convert the word into a (unique characters) hashable key\n            #(see notes above for explanation)\n            curr = sorted(set(word))\n            curr = \'\'.join(curr)\n\n            #add count of this "similar string" instances\n            if curr in wordMap:\n                wordMap[curr]+=1\n            else:\n                wordMap[curr]=1\n\n        pairs = 0\n        #traverse through all "similar string" keys\n            #and add pairs using the unique combination formula\n            #(see notes above for explanation)\n        for word in wordMap:\n            count = wordMap[word]\n            pairs += (count* (count-1))//2\n\n        \n        return pairs\n        #Time Compelxity: O(N*K)\n        #Space Complexity: O(26) -> O(1)\n\n\n```\n\n> Please Leave a like if you liked the approach
['Python', 'Python3']