Skip to content

Commit 3c07ff6

Browse files
committed
premium leetcode
1 parent 4f9e51e commit 3c07ff6

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
8989
- [Break a Palindrome](BreakAPalindrome/break_a_palindrome.dart)
9090
- [Palindrome Linked List](PalindromeLinkedList/palindrome_linked_list.dart)
9191
- [Increasing Triplet Subsequence](IncreasingTripletSubsequence/increasing_triplet_subsequence.dart)
92+
- [Shortest Word Distance](ShortestWordDistance/shortest_word_distance.dart)
9293

9394
## Reach me via
9495

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
3+
-* Shortest Word Distance *-
4+
5+
6+
you’re given an array of strings and two different words.
7+
We need to return the shortest distance between these two words that appear in the input string.
8+
9+
Example:
10+
Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice"
11+
Output: 3
12+
Explanation:
13+
14+
Word “coding” occurs at position 4.
15+
Word “practice” occurs at position 1.
16+
The shortest distance is 3.
17+
Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
18+
Output: 1
19+
Explanation:
20+
21+
Word “coding” occurs at position 4.
22+
Word “makes” occurs at positions 2 and 5.
23+
Shortest distance is 5-4 = 1.
24+
25+
26+
*/
27+
import 'dart:math';
28+
29+
class A {
30+
// Time Complexity
31+
// The time complexity of the above code is O(N) since we’re iterating over the entire input string exactly once.
32+
//
33+
// Space Complexity
34+
// The space complexity of the above code is O(1) since we’re using only constant space.
35+
int shortestDistance(List<String> wordsDict, String word1, String word2) {
36+
int ans = double.maxFinite.toInt(), pos1 = -1, pos2 = -1;
37+
for (int i = 0; i < wordsDict.length; i++) {
38+
if (wordsDict[i] == word1) {
39+
pos1 = i;
40+
}
41+
if (wordsDict[i] == word2) {
42+
pos2 = i;
43+
}
44+
if (pos1 != -1 && pos2 != -1) {
45+
ans = min(ans, (pos1 - pos2).abs());
46+
}
47+
}
48+
return ans;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import "math"
4+
5+
/*
6+
7+
int shortestDistance(List<String> wordsDict, String word1, String word2) {
8+
int ans = double.maxFinite.toInt(), pos1 = -1, pos2 = -1;
9+
for (int i = 0; i < wordsDict.length; i++) {
10+
if (wordsDict[i] == word1) {
11+
pos1 = i;
12+
}
13+
if (wordsDict[i] == word2) {
14+
pos2 = i;
15+
}
16+
if (pos1 != -1 && pos2 != -1) {
17+
ans = min(ans, (pos1 - pos2).abs());
18+
}
19+
}
20+
return ans;
21+
}
22+
*/
23+
24+
func shortestDistance(wordsDict []string, word1 string, word2 string) int {
25+
ans := math.MaxInt32
26+
firstPosition := -1
27+
secondPosition := -1
28+
for i := 0; i < len(wordsDict); i++ {
29+
if wordsDict[i] == word1 {
30+
firstPosition = i
31+
}
32+
if wordsDict[i] == word2 {
33+
secondPosition = i
34+
}
35+
if firstPosition != -1 && secondPosition != -1 {
36+
ans = int(math.Min(float64(ans), math.Abs(float64(firstPosition)-float64(secondPosition))))
37+
}
38+
}
39+
return ans
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 🔥 Shortest Word Distance 🔥 || Premium Leetcode || with Explanation
2+
3+
## Approach
4+
5+
This is a straight-forward coding problem. The distance between any two positions i_1i1​ and i_2i2​ in an array is |i_1 – i_2|∣i1​−i2​∣. To find the shortest distance between word1 and word2, we need to traverse the input array and find all occurrences i_1i1​ and i_2i2​ of the two words, and check if |i_1 – i_2|∣i1​−i2​∣ is less than the minimum distance computed so far.
6+
7+
## Idea
8+
9+
Maintain two position variables for each word.
10+
Iterate in the string array and update two position variables accordingly.
11+
Our Answer is the minimum absolute difference between these position variables.
12+
13+
Time Complexity
14+
The time complexity of the above code is O(N) since we’re iterating over the entire input string exactly once.
15+
16+
Space Complexity
17+
The space complexity of the above code is O(1) since we’re using only constant space.
18+
19+
```dart
20+
class Solution {
21+
int shortestDistance(List<String> wordsDict, String word1, String word2) {
22+
int ans = double.maxFinite.toInt(), pos1 = -1, pos2 = -1;
23+
for (int i = 0; i < wordsDict.length; i++) {
24+
if (wordsDict[i] == word1) {
25+
pos1 = i;
26+
}
27+
if (wordsDict[i] == word2) {
28+
pos2 = i;
29+
}
30+
if (pos1 != -1 && pos2 != -1) {
31+
ans = min(ans, (pos1 - pos2).abs());
32+
}
33+
}
34+
return ans;
35+
}
36+
}
37+
```
38+
39+
```go
40+
func shortestDistance(wordsDict []string, word1 string, word2 string) int {
41+
ans := math.MaxInt32
42+
firstPosition := -1
43+
secondPosition := -1
44+
for i := 0; i < len(wordsDict); i++ {
45+
if wordsDict[i] == word1 {
46+
firstPosition = i
47+
}
48+
if wordsDict[i] == word2 {
49+
secondPosition = i
50+
}
51+
if firstPosition != -1 && secondPosition != -1 {
52+
ans = int(math.Min(float64(ans), math.Abs(float64(firstPosition)-float64(secondPosition))))
53+
}
54+
}
55+
return ans
56+
57+
}
58+
```

0 commit comments

Comments
 (0)