245. Shortest Word Distance III
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the shortest distance between two given words, word1 and word2, in an array of strings wordsDict. This is a variation of the "Shortest Word Distance" problem. The key difference here is that word1 and word2 can be the same word. If they are the same, we need to find the minimum distance between two different occurrences of that word in the array.
Solution
Shortest Word Distance III in Python
class Solution:
def shortestWordDistance(self, wordsDict: List[str], word1: str, word2: str) -> int:
n = len(wordsDict)
min_dist = n # Initialize with a value larger than any possible distance.
# Case 1: word1 and word2 are the same.
if word1 == word2:
last_idx = -1
for i, word in enumerate(wordsDict):
if word == word1:
# If we have seen this word before, calculate the distance.
if last_idx != -1:
min_dist = min(min_dist, i - last_idx)
# Update the last seen index.
last_idx = i
else:
# Case 2: word1 and word2 are different.
idx1, idx2 = -1, -1
for i, word in enumerate(wordsDict):
if word == word1:
idx1 = i
# If we have seen word2, calculate distance.
if idx2 != -1:
# i > idx2, so no abs() needed.
min_dist = min(min_dist, idx1 - idx2)
elif word == word2:
idx2 = i
# If we have seen word1, calculate distance.
if idx1 != -1:
# i > idx1, so no abs() needed.
min_dist = min(min_dist, idx2 - idx1)
return min_dist
Shortest Word Distance III in Java
class Solution {
public int shortestWordDistance(String[] wordsDict, String word1, String word2) {
int n = wordsDict.length;
int min_dist = n; // Initialize with a value larger than any possible distance.
// Case 1: word1 and word2 are the same.
if (word1.equals(word2)) {
int last_idx = -1;
for (int i = 0; i < n; i++) {
if (wordsDict[i].equals(word1)) {
// If we have seen this word before, calculate the distance.
if (last_idx != -1) {
min_dist = Math.min(min_dist, i - last_idx);
}
// Update the last seen index.
last_idx = i;
}
}
} else {
// Case 2: word1 and word2 are different.
int idx1 = -1, idx2 = -1;
for (int i = 0; i < n; i++) {
if (wordsDict[i].equals(word1)) {
idx1 = i;
// If we have seen word2, calculate distance.
if (idx2 != -1) {
// i > idx2, so no Math.abs() needed.
min_dist = Math.min(min_dist, idx1 - idx2);
}
} else if (wordsDict[i].equals(word2)) {
idx2 = i;
// If we have seen word1, calculate distance.
if (idx1 != -1) {
// i > idx1, so no Math.abs() needed.
min_dist = Math.min(min_dist, idx2 - idx1);
}
}
}
}
return min_dist;
}
}
Shortest Word Distance III in C++
class Solution {
public:
int shortestWordDistance(vector<string>& wordsDict, string word1, string word2) {
int n = wordsDict.size();
int min_dist = n; // Initialize with a value larger than any possible distance.
// Case 1: word1 and word2 are the same.
if (word1 == word2) {
int last_idx = -1;
for (int i = 0; i < n; ++i) {
if (wordsDict[i] == word1) {
// If we have seen this word before, calculate the distance.
if (last_idx != -1) {
min_dist = min(min_dist, i - last_idx);
}
// Update the last seen index.
last_idx = i;
}
}
} else {
// Case 2: word1 and word2 are different.
int idx1 = -1, idx2 = -1;
for (int i = 0; i < n; ++i) {
if (wordsDict[i] == word1) {
idx1 = i;
// If we have seen word2, calculate distance.
if (idx2 != -1) {
// i > idx2, so no abs() needed.
min_dist = min(min_dist, idx1 - idx2);
}
} else if (wordsDict[i] == word2) {
idx2 = i;
// If we have seen word1, calculate distance.
if (idx1 != -1) {
// i > idx1, so no abs() needed.
min_dist = min(min_dist, idx2 - idx1);
}
}
}
}
return min_dist;
}
};
Shortest Word Distance III in TypeScript
function shortestWordDistance(wordsDict: string[], word1: string, word2: string): number {
const n = wordsDict.length;
let min_dist = n; // Initialize with a value larger than any possible distance.
// Case 1: word1 and word2 are the same.
if (word1 === word2) {
let last_idx = -1;
for (let i = 0; i < n; i++) {
if (wordsDict[i] === word1) {
// If we have seen this word before, calculate the distance.
if (last_idx !== -1) {
min_dist = Math.min(min_dist, i - last_idx);
}
// Update the last seen index.
last_idx = i;
}
}
} else {
// Case 2: word1 and word2 are different.
let idx1 = -1;
let idx2 = -1;
for (let i = 0; i < n; i++) {
if (wordsDict[i] === word1) {
idx1 = i;
// If we have seen word2, calculate distance.
if (idx2 !== -1) {
// i > idx2, so no Math.abs() needed.
min_dist = Math.min(min_dist, idx1 - idx2);
}
} else if (wordsDict[i] === word2) {
idx2 = i;
// If we have seen word1, calculate distance.
if (idx1 !== -1) {
// i > idx1, so no Math.abs() needed.
min_dist = Math.min(min_dist, idx2 - idx1);
}
}
}
}
return min_dist;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.