Created
December 27, 2019 08:56
-
-
Save tifoaudii/13bfffc0b7d4edb66ca51010a286eab2 to your computer and use it in GitHub Desktop.
This file contains 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 getCharacterFromString(str: String) -> Dictionary<Character,Int> { | |
var stringDictionary = Dictionary<Character, Int>() | |
for (_,char) in str.enumerated() { | |
if !stringDictionary.keys.contains(char) { | |
stringDictionary[char] = 1 | |
} else { | |
stringDictionary[char]! += 1 | |
} | |
} | |
return stringDictionary | |
} | |
func isAnagram(a: String, b: String) -> Bool { | |
var result: Bool = true | |
var stringDictionaryA = Dictionary<Character, Int>() | |
var stringDictionaryB = Dictionary<Character,Int>() | |
stringDictionaryA = getCharacterFromString(str: a) | |
stringDictionaryB = getCharacterFromString(str: b) | |
for (_, dictionary) in stringDictionaryA.enumerated() { | |
if !stringDictionaryB.keys.contains(dictionary.key) { | |
result = false | |
break | |
} else { | |
if stringDictionaryB[dictionary.key] != dictionary.value { | |
result = false | |
break | |
} | |
} | |
} | |
return result | |
} | |
isAnagram(a: "aabb", b: "bbaa") | |
isAnagram(a: "abbb", b: "aaab") | |
isAnagram(a: "a bba", b: "abba") |
Author
tifoaudii
commented
Dec 27, 2019
import UIKit
func getArrayDictionaryFromString(a: String, b: String) -> [Dictionary<Character,Int>] {
var arrayDictionary = [Dictionary<Character,Int>()]
var stringDictionaryA = Dictionary<Character, Int>()
var stringDictionaryB = Dictionary<Character, Int>()
for index in 0..<a.count {
let charIndexFromStringA = a.index(a.startIndex, offsetBy: index)
let charIndexFromStringB = b.index(b.startIndex, offsetBy: index)
let charAFromIndex = a[charIndexFromStringA]
let charBFromIndex = b[charIndexFromStringB]
if !stringDictionaryA.keys.contains(charAFromIndex) {
stringDictionaryA[charAFromIndex] = 1
} else {
stringDictionaryA[charAFromIndex]! += 1
}
if !stringDictionaryB.keys.contains(charBFromIndex) {
stringDictionaryB[charBFromIndex] = 1
} else {
stringDictionaryB[charBFromIndex]! += 1
}
}
arrayDictionary.append(stringDictionaryA)
arrayDictionary.append(stringDictionaryB)
return arrayDictionary
}
func isAnagram(a: String, b: String) -> Bool {
var result: Bool = true
var stringDictionaryA = Dictionary<Character, Int>()
var stringDictionaryB = Dictionary<Character,Int>()
if a.count != b.count {
result = false
return result
} else {
let arrayDictionary = getArrayDictionaryFromString(a: a, b: b)
stringDictionaryA = arrayDictionary[1]
stringDictionaryB = arrayDictionary[2]
for (_, dictionary) in stringDictionaryA.enumerated() {
if !stringDictionaryB.keys.contains(dictionary.key) {
result = false
break
} else {
if stringDictionaryB[dictionary.key] != dictionary.value {
result = false
break
}
}
}
return result
}
}
isAnagram(a: "aabb", b: "bbaa")
isAnagram(a: "abbb", b: "aaab")
isAnagram(a: "a bba", b: "abba")
isAnagram(a: "AbbA", b: "abba")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment