forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneEditDistance.swift
40 lines (34 loc) · 1.15 KB
/
OneEditDistance.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 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
}
}