In this post, Stefan's solutions are very concise. However, I found that the following solution only runs 280ms on OJ, which beats all other java solutions so far. It is fast because it only checks word1.equals(word2) once at the beginning. I wonder in real practice, do we prefer cleaner code or faster solution if we cannot achieve both with one solution? I hope people who have more industrial experience could give some adivce.

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