Swift | Simple Set && Self-made hash solutions
Simple Set\n## Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(n)$\n$n$ is words count\n\n\nclass Solution {\n    func maximumNumberOfStringPairs(
# Simple Set\n## Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(n)$\n$n$ is words count\n\n```\nclass Solution {\n    func maximumNumberOfStringPairs(_ words: [String]) -> Int\n    {\n        var wordSet: Set<String> = [], count = 0\n        for word in words\n        {\n            if wordSet.contains(String(word.reversed())) { count += 1 }\n            else { wordSet.insert(word) }\n        }\n        return count\n    }\n}\n```\n\n# Custom hash\n\nAs the words are constained to have just 2 letters, we can do without the Set with its costly hash calculation. Just use ascii values of two letters, which can produce 26 * 26 = 676 combinations and map these combinations to a bit array.\nAlso when calculating hash we first bring the word\'s letters to ascending order. This conveniently ensures that reversals, i.e. pair words that we need to count will have the same hash value.\n\n## Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(26^m)$\n$n$ is words count, m is words letter count (currently 2)\n\n```\nclass Solution {\n    func maximumNumberOfStringPairs(_ words: [String]) -> Int\n    {\n        var wordSet: [UInt8] = Array(repeating: 0, count: 26*26/8 + 1)\n        let a: UInt8 = 97 //asciiValue\n\n        func hash(_ word: String) -> Int\n        {\n            var chars = Array(word)\n            if chars[0] > chars[1] { chars.swapAt(0, 1) }\n            return Int(chars[0].asciiValue! - a) * 26 + Int(chars[1].asciiValue! - a)\n        }\n        func contains(_ word: String) -> Bool {\n            let hashValue = hash(word)\n            return wordSet[hashValue / 8] & (1 << (hashValue % 8)) != 0\n        }\n        func insert(_ word: String) {\n            let hashValue = hash(word)\n            wordSet[hashValue / 8] |= (1 << (hashValue % 8))\n        }\n        \n        var count = 0\n        for word in words\n        {\n            if contains(word) { count += 1 }\n            else { insert(word) }\n        }\n        return count\n    }\n}\n```
['Hash Table', 'Bit Manipulation', 'Swift', 'Hash Function']