Created
April 10, 2016 03:04
-
-
Save ayunav/3a60ad3502c19658c5849918ebe86b7d to your computer and use it in GitHub Desktop.
Determine if two strings are anagrams of each other
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func stringIsAnagram(str1: String, str2 : String) -> Bool { | |
| var dict1 = [Character: Int]() | |
| var dict2 = [Character: Int]() | |
| var count = 0 | |
| for char in str1.characters { | |
| if (dict1[char] != nil) { | |
| dict1[char] = count++ | |
| } else { | |
| dict1[char] = count | |
| } | |
| } | |
| for char in str2.characters { | |
| if (dict2[char] != nil) { | |
| dict2[char] = count++ | |
| } else { | |
| dict2[char] = count | |
| } | |
| } | |
| if dict1 == dict2 { | |
| return true | |
| } | |
| return false | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment