package com.fishercoder.solutions;

/**
 * This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as word2.

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

 word1 and word2 may be the same and they represent two individual words in the list.

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

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

 Note:
 You may assume word1 and word2 are both in the list.
 */
public class _245 {

    public int shortestWordDistance(String[] words, String word1, String word2) {
        int p1 = -1;
        int p2 = -1;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < words.length; i++) {
            if (words[i].equals(word1)) {
                if (word1.equals(word2)) {
                    if (p1 != -1 && i - p1 < min) {
                        min = i - p1;
                    }
                    p1 = i;
                } else {
                    p1 = i;
                    if (p2 != -1 && p1 - p2 < min) {
                        min = p1 - p2;
                    }
                }
            } else if (words[i].equals(word2)) {
                p2 = i;
                if (p1 != -1 && p2 - p1 < min) {
                    min = p2 - p1;
                }
            }
        }
        return min;
    }

}

@@ -33,16 +33,54 @@
 s;%0A%0A/**%0A
+ * 245. Shortest Word Distance III%0A *%0A
  * This 
@@ -182,17 +182,18 @@
  word2.%0A
-%0A
+ *
  Given a
@@ -304,17 +304,18 @@
 e list.%0A
-%0A
+ *
  word1 a
@@ -672,16 +672,56 @@
 _245 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -798,24 +798,28 @@
 ) %7B%0A        
+    
 int p1 = -1;
@@ -823,24 +823,28 @@
 -1;%0A        
+  
+  
 int p2 = -1;
@@ -848,24 +848,28 @@
 -1;%0A        
+   
+ 
 int min = In
@@ -885,16 +885,20 @@
 _VALUE;%0A
+    
         
@@ -942,32 +942,36 @@
 ) %7B%0A            
+ 
+   
 if (words%5Bi%5D.equ
@@ -992,32 +992,36 @@
                 
+   
+ 
 if (word1.equals
@@ -1023,32 +1023,36 @@
 quals(word2)) %7B%0A
+    
                 
@@ -1107,24 +1107,28 @@
             
+    
 min = i - p1
@@ -1141,34 +1141,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -1199,24 +1199,28 @@
             
+    
 %7D else %7B%0A   
@@ -1232,32 +1232,36 @@
             
+   
+ 
 p1 = i;%0A
             
@@ -1240,32 +1240,36 @@
         p1 = i;%0A
+    
                 
@@ -1325,24 +1325,28 @@
             
+    
 min = p1 - p
@@ -1360,34 +1360,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
                 
@@ -1388,32 +1388,36 @@
               %7D%0A
+    
             %7D el
@@ -1469,16 +1469,20 @@
     
+  
+  
 p2 = i;%0A
     
@@ -1473,24 +1473,28 @@
     p2 = i;%0A
+    
             
@@ -1546,24 +1546,28 @@
             
+    
 min = p2 - p
@@ -1577,34 +1577,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
             %7D%0A  
@@ -1617,26 +1617,32 @@
         
-%7D%0A
+    %7D%0A  
         
 return m
@@ -1633,16 +1633,18 @@
         
+  
 return m
@@ -1651,13 +1651,22 @@
 in;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 245