★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gtxbowvh-md.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given two strings S and T, determine if they are both one edit distance apart.git
給定兩個字符串S和T,肯定它們是否都是是一步變換獲得的。github
class Solution { func isOneEditDistance(_ s:String, _ t:String) -> Bool{ var num:Int = min(s.count, t.count) for i in 0..<num { if s.count == t.count { return s.substring(i + 1) == t.substring(i + 1) } else if s.count < t.count { return s.substring(i) == t.substring(i + 1) } else { return s.substring(i + 1) == t.substring(i) } } return abs(s.count - t.count) == 1 } } extension String { // 截取字符串:從index到結束處 // - Parameter index: 開始索引 // - Returns: 子字符串 func substring(_ index: Int) -> String { let theIndex = self.index(self.endIndex, offsetBy: index - self.count) return String(self[theIndex..<endIndex]) } }