Swift 中带有通配符(或百搭)的字谜
Anagrams in Swift with wildcard (or joker)
基于Swift中的这个函数:
func anagrams(word: String, from words: [String]) -> [String] {
let anagrammedWord = word as NSString
let length = anagrammedWord.length
var aDic = [unichar:Int]()
for i in 0..<length {
let c = anagrammedWord.character(at: i)
aDic[c] = (aDic[c] ?? 0) + 1
}
let foundWords = words.filter {
let string = [=11=] as NSString
guard length == string.length else { return false }
var bDic = [unichar:Int]()
for i in 0..<length {
let c = string.character(at: i)
let count = (bDic[c] ?? 0) + 1
if count > aDic[c] ?? 0 {
return false
}
bDic[c] = count
}
return true
}
return foundWords
}
我试图让这个与通配符一起工作,但它没有。
如何使用 ?
或 _
检查?
编辑:
它是这样工作的:
input : anagrams(word: "MILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"])
output : ["MILK", "LIMK"]
我想使用通配符,例如:
input : anagrams(word: "?ILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"])
output : ["MILK", "LINK", "LIMK"]
你可以使用这个:
func anagrams(word: String, from words: [String]) -> [String] {
let characterCount = word.count
let histogramOfWord = histogram(of: word)
let foundWords = words.filter { otherWord in
//Exit early if the character counts differ
if otherWord.count != characterCount {
return false
}
let h2 = histogram(of: otherWord)
//If they have similar histograms, count that word in
if h2 == histogramOfWord {
return true
}
//Compare the histograms with wildcards taken into consideration
else {
guard let wildCards = histogramOfWord["?"], wildCards > 0 else {
return false
}
let numberOfDifferences: Int = h2.map { entry in
let key = entry.key
let value1 = histogramOfWord[key] ?? 0
let value2 = entry.value
let difference = value2 - value1
return difference >= 0 ? difference : 0
}.reduce(0, +)
return numberOfDifferences == wildCards
}
}
return foundWords
}
它调用这个函数来计算字符串中每个字符的频率:
func histogram(of string: String) -> [Character:Int] {
var dictionary: [Character:Int] = [Character:Int]()
for i in 0..<string.count {
let c = string[string.index(string.startIndex, offsetBy: i)]
dictionary[c] = (dictionary[c] ?? 0) + 1
}
return dictionary
}
你可以这样使用它:
print(anagrams(word: "MILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"]))
//["MILK", "LIMK"]
print(anagrams(word: "?ILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"]))
//["MILK", "LINK", "LIMK"]
print(anagrams(word: "TREE?", from: ["ARETE", "BERET", "BARTE", "MILKA"]))
//["ARETE", "BERET"]
print(anagrams(word: "TREE", from: ["ATRE", "CRET", "LINK", "RETE", "TREE"]))
//["RETE", "TREE"]
基于Swift中的这个函数:
func anagrams(word: String, from words: [String]) -> [String] {
let anagrammedWord = word as NSString
let length = anagrammedWord.length
var aDic = [unichar:Int]()
for i in 0..<length {
let c = anagrammedWord.character(at: i)
aDic[c] = (aDic[c] ?? 0) + 1
}
let foundWords = words.filter {
let string = [=11=] as NSString
guard length == string.length else { return false }
var bDic = [unichar:Int]()
for i in 0..<length {
let c = string.character(at: i)
let count = (bDic[c] ?? 0) + 1
if count > aDic[c] ?? 0 {
return false
}
bDic[c] = count
}
return true
}
return foundWords
}
我试图让这个与通配符一起工作,但它没有。
如何使用 ?
或 _
检查?
编辑:
它是这样工作的:
input : anagrams(word: "MILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"])
output : ["MILK", "LIMK"]
我想使用通配符,例如:
input : anagrams(word: "?ILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"])
output : ["MILK", "LINK", "LIMK"]
你可以使用这个:
func anagrams(word: String, from words: [String]) -> [String] {
let characterCount = word.count
let histogramOfWord = histogram(of: word)
let foundWords = words.filter { otherWord in
//Exit early if the character counts differ
if otherWord.count != characterCount {
return false
}
let h2 = histogram(of: otherWord)
//If they have similar histograms, count that word in
if h2 == histogramOfWord {
return true
}
//Compare the histograms with wildcards taken into consideration
else {
guard let wildCards = histogramOfWord["?"], wildCards > 0 else {
return false
}
let numberOfDifferences: Int = h2.map { entry in
let key = entry.key
let value1 = histogramOfWord[key] ?? 0
let value2 = entry.value
let difference = value2 - value1
return difference >= 0 ? difference : 0
}.reduce(0, +)
return numberOfDifferences == wildCards
}
}
return foundWords
}
它调用这个函数来计算字符串中每个字符的频率:
func histogram(of string: String) -> [Character:Int] {
var dictionary: [Character:Int] = [Character:Int]()
for i in 0..<string.count {
let c = string[string.index(string.startIndex, offsetBy: i)]
dictionary[c] = (dictionary[c] ?? 0) + 1
}
return dictionary
}
你可以这样使用它:
print(anagrams(word: "MILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"]))
//["MILK", "LIMK"]
print(anagrams(word: "?ILK", from: ["EGGS", "MILK", "LINK", "LIMK", "TREE"]))
//["MILK", "LINK", "LIMK"]
print(anagrams(word: "TREE?", from: ["ARETE", "BERET", "BARTE", "MILKA"]))
//["ARETE", "BERET"]
print(anagrams(word: "TREE", from: ["ATRE", "CRET", "LINK", "RETE", "TREE"]))
//["RETE", "TREE"]