题目:给定两个字符串,确定其中一个字符串重新排列之后,是否能变成另外一个字符串.
核心代码:
<pre><code>` func permutation(strA:String,strB:String) -> Bool {
if strA.characters.count != strB.characters.count {
return false
}
var arr:[Int] = [Int].init(repeating: 0, count: 256)
for i in 0..<strA.characters.count {
let temp:String = strA[i] as String
let index:Int = temp.toInt()
arr[index] = arr[index] + 1
}
for i in 0..<strB.characters.count {
let temp:String = strA[i] as String
let index:Int = temp.toInt()
if arr[index] - 1 < 0 {
return false
}
}
return true
}`<code></pre>
测试代码:
<pre><code>var strA:String = "student" var strB:String = "FlyElephant" var perResult:Bool = myString.permutation(strA: strA, strB: strB) print("\(strA)与\(strB)是不是变位词\(perResult)")
</code></pre>