2 Star 2 Fork 1

Justin Yuan/LeetCode-Swift

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
OneEditDistance.swift 1.15 KB
一键复制 编辑 原始数据 按行查看 历史
/**
* Question Link: https://leetcode.com/problems/one-edit-distance/
* Primary idea: Two pointers to determine two strings' mutation
* Time Complexity: O(n), Space Complexity: O(n)
*/
class OneEditDistance {
func isOneEditDistance(_ s: String, _ t: String) -> Bool {
let sChars = Array(s.characters), tChars = Array(t.characters)
var foundDiff = false, i = 0, j = 0
let shorter = sChars.count < tChars.count ? sChars : tChars
let longer = sChars.count < tChars.count ? tChars : sChars
guard longer.count - shorter.count < 2 && s != t else {
return false
}
while i < shorter.count && j < longer.count {
if shorter[i] != longer[j] {
if foundDiff {
return false
}
foundDiff = true
if shorter.count < longer.count {
j += 1
} else {
i += 1
j += 1
}
} else {
i += 1
j += 1
}
}
return true
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/BiggerMax/LeetCode-Swift.git
git@gitee.com:BiggerMax/LeetCode-Swift.git
BiggerMax
LeetCode-Swift
LeetCode-Swift
master

搜索帮助