-
Notifications
You must be signed in to change notification settings - Fork 900
Expand file tree
/
Copy pathShortestWordDistance.swift
More file actions
29 lines (24 loc) · 917 Bytes
/
ShortestWordDistance.swift
File metadata and controls
29 lines (24 loc) · 917 Bytes
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
/**
* Question Link: https://leetcode.com/problems/shortest-word-distance/
* Primary idea: Iterate and update index and distance when encounter word1 or word2
* Time Complexity: O(n), Space Complexity: O(1)
*/
class ShortestWordDistance {
func shortestDistance(_ words: [String], _ word1: String, _ word2: String) -> Int {
var minDistance = Int.max, word1Idx = -1, word2Idx = -1
for (i, word) in words.enumerated() {
if word == word1 {
word1Idx = i
if word2Idx != -1 {
minDistance = min(i - word2Idx, minDistance)
}
} else if word == word2 {
word2Idx = i
if word1Idx != -1 {
minDistance = min(i - word1Idx, minDistance)
}
}
}
return minDistance
}
}