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
45. Jump Game II - Detailed Explanation
Learn to solve Leetcode 45. Jump Game II with multiple approaches.
3264. Final Array State After K Multiplication Operations I - Detailed Explanation
Learn to solve Leetcode 3264. Final Array State After K Multiplication Operations I with multiple approaches.
75. Sort Colors - Detailed Explanation
Learn to solve Leetcode 75. Sort Colors with multiple approaches.
316. Remove Duplicate Letters - Detailed Explanation
Learn to solve Leetcode 316. Remove Duplicate Letters with multiple approaches.
48. Rotate Image - Detailed Explanation
Learn to solve Leetcode 48. Rotate Image with multiple approaches.
695. Max Area of Island - Detailed Explanation
Learn to solve Leetcode 695. Max Area of Island 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
(69,299 learners)
$197
New

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.