package com.fishercoder.solutions;

/**Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.

 For example,
 Assume that words = ["practice", "makes", "perfect", "coding", "makes"].

 Given word1 = “coding”, word2 = “practice”, return 3.
 Given word1 = "makes", word2 = "coding", return 1.

 Note:
 You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.*/
public class _243 {

    public int shortestDistance(String[] words, String word1, String word2) {

        int p = -1;
        int q = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < words.length; i++) {
            if (words[i].equals(word1)) {
                p = i;
            }
            if (words[i].equals(word2)) {
                q = i;
            }
            if (p != -1 && q != -1) {
                min = Math.min(min, Math.abs(p - q));
            }
        }
        return min;

    }

}

@@ -32,16 +32,54 @@
 ns;%0A%0A/**
+%0A * 243. Shortest Word Distance%0A *%0A * 
 Given a 
@@ -510,17 +510,56 @@
  _243 %7B%0A
-%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -628,17 +628,20 @@
 ord2) %7B%0A
-%0A
+    
         
@@ -656,24 +656,28 @@
 -1;%0A        
+    
 int q = -1;%0A
@@ -680,24 +680,28 @@
 -1;%0A        
+ 
+   
 int min = In
@@ -725,16 +725,20 @@
         
+   
+ 
 for (int
@@ -774,32 +774,36 @@
 ) %7B%0A            
+    
 if (words%5Bi%5D.equ
@@ -832,16 +832,20 @@
         
+  
+  
 p = i;%0A 
@@ -847,32 +847,36 @@
  i;%0A            
+    
 %7D%0A            if
@@ -865,32 +865,36 @@
   %7D%0A            
+ 
+   
 if (words%5Bi%5D.equ
@@ -923,16 +923,20 @@
         
+    
 q = i;%0A 
@@ -938,34 +938,40 @@
  i;%0A            
-%7D%0A
+    %7D%0A  
             if (
@@ -962,24 +962,26 @@
             
+  
 if (p != -1 
@@ -990,24 +990,28 @@
  q != -1) %7B%0A
+    
             
@@ -1056,34 +1056,42 @@
 ));%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -1092,16 +1092,20 @@
         
+    
 return m
@@ -1113,13 +1113,22 @@
 n;%0A%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 243