✅ 2 Best Swift Solutions | Easy To Understand
The first solution using a Dictionary\n\n## Approach\nFirst, the function checks if the lengths of the two strings s and t are equal. If not, the function retur
# The first solution using a Dictionary\n\n## Approach\nFirst, the function checks if the lengths of the two strings `s` and `t` are equal. If not, the function returns false as the two strings cannot be anagrams if they have different lengths.\n\nThen, the function creates an empty dictionary to store the frequency of each character in `s`. It loops through every character in `s` and increments the frequency of the character in the dictionary by `1`.\n\nNext, the function loops through every character in `t`. For each character, it checks if the character exists in the dictionary and has a frequency greater than `0`. If so, it decrements the frequency of the character in the dictionary. If not, the function returns `false` as the two strings cannot be anagrams.\n\nIf the function reaches the end of the `t` loop without returning `false`, it means that all characters in `t` are found in `s` and have the same frequency. Therefore, the function returns `true` as the two strings are anagrams.\n\n\n## Complexity\nThis approach has a time complexity of $$O(n)$$, where $$n$$ is the length of the input strings.\n\nThe space complexity of the function is $$O(k)$$, where $$k$$ is the number of unique characters in the input strings.\n\nIt is an efficient way to solve the anagram problem as it only requires one pass through each string and uses a dictionary to keep track of character frequencies.\n\n## Code\n```\nclass Solution {\n    func isAnagram(_ s: String, _ t: String) -> Bool {\n        guard s.count == t.count else { return false }\n\n        var dict = [Character: Int]()\n\n        for char in s {\n            dict[char, default: 0] += 1\n        }\n\n        for char in t {\n            if let count = dict[char], count > 0 {\n                dict[char] = count - 1\n            } else {\n                return false\n            }\n        }\n\n        return true\n    }\n}\n```\n\n\n---\n\n\n\n# The second solution using String sorting\n\n## Approach\nThe idea behind this approach is to sort both strings and compare them. If both sorted strings are equal, then the original strings are anagrams of each other. \n\nIn the implementation, `s.sorted()` and `t.sorted()` returns an *array of characters* that are sorted. Then, we use `==` operator to compare the two sorted arrays of characters. \n\nIf `s` and `t` are anagrams of each other, then `s.sorted()` and `t.sorted()` should have the same characters in the same order, which means the result of `s.sorted() == t.sorted()` should be `true`. \n\nIf they are not anagrams, the result will be `false`.\n\n## Complexity\nThis approach has a time complexity of $$O(n \\cdot \\log n)$$ due to the sorting of the strings, where $$n$$ is the length of the input strings. The space complexity is $$O(n)$$ because we need to create arrays of characters to store the sorted strings.\n\n## Code\n```\nclass Solution {\n    func isAnagram(_ s: String, _ t: String) -> Bool {\n        s.sorted() == t.sorted()\n    }\n}\n```\n\n# Upvote ^_^\n\n![upvote.png](https://assets.leetcode.com/users/images/0dc281d0-064a-47f1-b845-e07c6d11f7a4_1681545226.447748.png)\n\n
['Hash Table', 'String', 'Swift', 'Sorting']