Add (3) lines to solution 243, w/ Explanations


  • 1
    public class Solution {
        public int shortestWordDistance(String[] words, String word1, String word2) {
            int ans = Integer.MAX_VALUE;
            int p = -1; //POS for word1
            int q = -1; //POS for word2
            
            for(int i = 0; i < words.length; i++){
                if(words[i].equals(word1) && words[i].equals(word2)){
                    p = q;
                    q = i; 
                    //slide two pointer, q is always the front pos when word1 == word2
                }
                else{
                    if(words[i].equals(word1)){
                        p = i;
                    }
                    if(words[i].equals(word2)){
                        q = i;
                    }
                }
                if(p!=-1 && q!=-1){
                    ans = Math.min(ans,Math.abs(p-q));
                }
            }
            
            //Proof: word1 and word2 are both in the list. 
            //Whenever update p/q, the existing q/p is the nearest one to the left.
            
            return ans;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.