题目:输入两个字符串,从第一个字符串中删除在第二个中出现过的所偶字符串。例如从第一个字符串"We are students."中删除第二个字符中“aeiou”中出现过的字符得到的结果是"W r stdnts".
核心代码:
<pre><code>`
func compareDelete(origin:String,delte:String) -> String {
if origin.characters.count == 0 {
return ""
}
var dict:[String:Int] = [:]
for i in 0..<delte.characters.count {
let char:String = delte[i]
if dict[char] == nil {
dict[char] = 1
}
}
var result:String = ""
for i in 0..<origin.characters.count {
let char:String = origin[i]
if dict[char] == nil {
result += char
}
}
return result
}`</code></pre>
测试代码:
<pre><code>var result:String = hashSearch.compareDelete(origin: "We are students.", delte: "aeiou") print("FlyElephatn-对比删除之后的字符串---\(result)")
</code></pre>
网友评论