剑指 Offer II 014. 字符串中的变位词
作者:
邦_ | 来源:发表于
2022-04-13 09:05 被阅读0次
func checkInclusion(_ s1: String, _ s2: String) -> Bool {
let m = s1.count
let n = s2.count
if m > n {
return false
}
let tempValue :Character = "a"
var array1 = Array<Int>(repeating: 0, count: 26)
var array2 = Array<Int>(repeating: 0, count: 26)
let temp1 = Array(s1)
let temp2 = Array(s2)
for i in 0..<temp1.count {
array1[Int((temp1[i].asciiValue ?? 0) - (tempValue.asciiValue ?? 0))] += 1
array2[Int((temp2[i].asciiValue ?? 0) - (tempValue.asciiValue ?? 0))] += 1
}
if array1 == array2 {
return true
}
for i in m..<n {
array2[Int((temp2[i].asciiValue ?? 0) - (tempValue.asciiValue ?? 0))] += 1
array2[Int((temp2[i - m].asciiValue ?? 0) - (tempValue.asciiValue ?? 0))] -= 1
if array1 == array2 {
return true
}
}
return false
}
本文标题:剑指 Offer II 014. 字符串中的变位词
本文链接:https://www.haomeiwen.com/subject/qsmysrtx.html
网友评论