-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
161 One Edit Distance.swift
48 lines (38 loc) · 1.26 KB
/
161 One Edit Distance.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
41
42
43
44
45
46
47
48
//
// 161 One Edit Distance.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 22/01/2021.
// Copyright © 2021 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/one-edit-distance/
class Solution {
/// Finds if given two strings are one edit distance apart.
///
/// - Parameters:
/// - s: First string.
/// - t: Second string.
/// - Returns: True if given strings are one edit distance apart, otherwise
/// returns false.
///
/// - Complexity:
/// - time: O(n), where n is the max length of `s` and `t`.
/// - space: O(n), where n is the max length of `s` and `t`.
func isOneEditDistance(_ s: String, _ t: String) -> Bool {
let n = s.count
let m = t.count
guard n <= m else { return isOneEditDistance(t, s) }
guard n == m || m - n == 1 else { return false }
let s = Array(s)
let t = Array(t)
for i in 0..<n {
guard s[i] != t[i] else { continue }
if n == m {
return s[i+1..<n] == t[i+1..<m]
}
return s[i..<n] == t[i+1..<m]
}
return n + 1 == m
}
}