forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_243.java
More file actions
25 lines (22 loc) · 674 Bytes
/
_243.java
File metadata and controls
25 lines (22 loc) · 674 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.fishercoder.solutions;
public class _243 {
public static class Solution1 {
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;
}
}
}