Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Shortest distance between words
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static int shortestDistBetweenWords(String[] list,String word1,String word2){ int index1 = -1; int index2 = -1; int minDist = Integer.MAX_VALUE; for(int i = 0;i<list.length;i++){ if(list[i]==word1){ index1 = i; if(index2!=-1){ minDist = Math.min(minDist,Math.abs(index2-index1)); } } else if(list[i]==word2){ index2 = i; if(index1!=-1){ minDist = Math.min(minDist,Math.abs(index2-index1)); } } } return minDist; } public static void main(String args[]) { System.out.println("Hello, World!"); System.out.println(shortestDistBetweenWords(new String[]{"quick","brown","jumped","the","fox"},"quick","fox")); } }
run
|
edit
|
history
|
help
0
JAVA regex for only allow numbers
exp 3
susun
Huffman Encoding Tree
191fa07050
rstring
mp1
Java tester
fst
Borrowing class