File tree 2 files changed +18
-0
lines changed
2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change
1
+ package easy ;
2
+
3
+ public class ShortestWordDistance {
4
+
5
+ public int shortestDistance (String [] words , String word1 , String word2 ) {
6
+
7
+ int p = -1 , q = -1 , min = Integer .MAX_VALUE ;
8
+ for (int i = 0 ; i < words .length ; i ++){
9
+ if (words [i ].equals (word1 )) p = i ;
10
+ if (words [i ].equals (word2 )) q = i ;
11
+ if (p != -1 && q != -1 ) min = Math .min (min , Math .abs (p -q ));
12
+ }
13
+ return min ;
14
+
15
+ }
16
+
17
+ }
Original file line number Diff line number Diff line change 61
61
|257|[ Binary Tree Paths] ( https://leetcode.com/problems/binary-tree-paths/ ) |[ Solution] ( ../../blob/master/EASY/src/easy/BinaryTreePaths.java ) | O(n* h) | O(h) | DFS/Recursion
62
62
| 252| [ Meeting Rooms] ( https://leetcode.com/problems/meeting-rooms/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/MeetingRooms.java ) | O(nlogn) | O(1) |
63
63
| 246| [ Strobogrammatic Number] ( https://leetcode.com/problems/strobogrammatic-number/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/StrobogrammaticNumber.java ) | O(n) | O(1) |
64
+ | 243| [ Shortest Word Distance] ( https://leetcode.com/problems/shortest-word-distance/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/ShortestWordDistance.java ) | O(n) | O(1) |
64
65
| 223| [ Rectangle Area] ( https://leetcode.com/problems/rectangle-area/ ) | [ Solution] ( ../../blob/master/EASY/src/easy/RectangleArea.java ) | O(1)| O(1) | Easy|
65
66
|219|[ Contains Duplicate II] ( https://leetcode.com/problems/contains-duplicate-ii/ ) |[ Solution] ( ../../blob/master/EASY/src/easy/ContainsDuplicateII.java ) | O(n)|O(n) | Easy| HashMap
66
67
| 209| [ Minimum Size Subarray Sum] ( https://leetcode.com/problems/minimum-size-subarray-sum/ ) | [ Solution] ( ../../blob/master/MEDIUM/src/medium/MinimumSizeSubarraySum.java ) | O(n)| O(1) | Medium|
You can’t perform that action at this time.
0 commit comments