243. Shortest Word Distance - Detailed Explanation
Problem Statement
You’re given an array of strings words and two distinct strings word1 and word2 that both appear in words. Return the shortest distance (in indices) between any occurrence of word1 and any occurrence of word2. Formally, find the minimum value of |i – j| such that words[i] == word1 and words[j] == word2.
Examples
Example 1
Input
words = ["practice","makes","perfect","coding","makes"]
word1 = "coding", word2 = "practice"
Output
3
Explanation
"coding" is at index 3 and "practice" at index 0 → |3 − 0| = 3
Example 2
Input
words = ["practice","makes","perfect","coding","makes"]
word1 = "makes", word2 = "coding"
Output
1
Explanation
Closest "makes" at index 4 and "coding" at index 3 → |4 − 3| = 1
Constraints
2 ≤ words.length ≤ 3·10⁴1 ≤ words[i].length ≤ 10word1 != word2- Both
word1andword2are guaranteed to appear inwords.
Hints
- Brute force checking every pair of positions is O(n²) and too slow for large n.
- You only need to remember the last seen index of each target word as you scan.
- Update the minimum distance whenever you encounter either target and you have seen the other at least once.
Approach
One‑Pass with Two Pointers
- Initialize
idx1 = -1,idx2 = -1, andminDist = +∞. - Loop
ifrom0towords.length-1:- If
words[i] == word1, setidx1 = i. - Else if
words[i] == word2, setidx2 = i. - If both
idx1andidx2are ≥ 0, updateminDist = min(minDist, |idx1 - idx2|)
- If
- Return
minDistat the end.
This keeps it O(n) time and O(1) extra space.
Step‑by‑Step (Example 2)
words = ["practice","makes","perfect","coding","makes"]
word1="makes", word2="coding"
i=0: "practice" → no change
i=1: "makes" → idx1=1, idx2=-1 → can't update yet
i=2: "perfect" → no change
i=3: "coding" → idx2=3, idx1=1 → minDist = |3-1|=2
i=4: "makes" → idx1=4, idx2=3 → minDist = min(2,|4-3|=1) = 1
Return 1
Complexity Analysis
- Time: O(n), a single pass through
words. - Space: O(1), only a few integer variables.
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Forgetting to check distance right after updating an index.
- Using two separate loops instead of a single scan (still correct but less concise).
- Initializing
minDistto 0 instead of a large value, causing incorrect early minima.
Edge Cases
- The first occurrence of one word appears at index 0 and the other at the very end.
- Multiple interleaved occurrences—ensure you always compare to the most recent other index.
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
129. Sum Root to Leaf Numbers - Detailed Explanation
Learn to solve Leetcode 129. Sum Root to Leaf Numbers with multiple approaches.
1492. The kth Factor of n - Detailed Explanation
Learn to solve Leetcode 1492. The kth Factor of n with multiple approaches.
1424. Diagonal Traverse II - Detailed Explanation
Learn to solve Leetcode 1424. Diagonal Traverse II with multiple approaches.
148. Sort List - Detailed Explanation
Learn to solve Leetcode 148. Sort List with multiple approaches.
1868. Product of Two Run-Length Encoded Arrays - Detailed Explanation
Learn to solve Leetcode 1868. Product of Two Run-Length Encoded Arrays with multiple approaches.
214. Shortest Palindrome - Detailed Explanation
Learn to solve Leetcode 214. Shortest Palindrome with multiple approaches.
Related Courses

Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
$78

Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
$78
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.