Given an array of strings wordsDict
and two different strings that already exist in the array word1
and word2
, return the shortest distance between these two words in the list.
Example 1:
Input: wordsDict = [ "practice ", "makes ", "perfect ", "coding ", "makes "], word1 = "coding ", word2 = "practice " Output: 3
Example 2:
Input: wordsDict = [ "practice ", "makes ", "perfect ", "coding ", "makes "], word1 = "makes ", word2 = "coding " Output: 1
Constraints:
2 <= wordsDict.length <= 3 * 104
1 <= wordsDict[i].length <= 10
wordsDict[i]
consists of lowercase English letters.word1
and word2
are in wordsDict
.word1 != word2
program main
implicit none
integer, parameter :: n = 5
character(len=10), dimension(n) :: wordsDict = ["practice ", "makes ", "perfect ", "coding ", "makes "]
character(len=10), intent(in) :: word1, word2
integer :: dist
call solve(wordsDict, word1, word2, dist)
print '(I0)', dist
contains
subroutine solve(wordsDict, word1, word2, dist)
implicit none
character(len=10), dimension(:), intent(in) :: wordsDict
character(len=10), intent(in) :: word1, word2
integer, intent(out) :: dist
integer :: i, j, k
do i = 1, size(wordsDict)
if (wordsDict(i) == word1) then
do j = 1, size(wordsDict)
if (wordsDict(j) == word2) then
dist = min(dist, i - j)
end if
end do
end if
end do
end subroutine solve
end program main
temp.f95:5:64: 5 | character(len=10), dimension(n) :: wordsDict = ["practice ", "makes ", "perfect ", "coding ", "makes "] | 1 Error: Different CHARACTER lengths (9/6) in array constructor at (1) temp.f95:6:42: 6 | character(len=10), intent(in) :: word1, word2 | 1 Error: Symbol at (1) is not a DUMMY variable temp.f95:6:49: 6 | character(len=10), intent(in) :: word1, word2 | 1 Error: Symbol at (1) is not a DUMMY variable temp.f95:9:24: 9 | call solve(wordsDict, word1, word2, dist) | 1 Error: Symbol ‘wordsdict’ at (1) has no IMPLICIT type
module shortest_distance
implicit none
contains
function shortest_distance(wordsDict, word1, word2) result(shortest_dist)
! Declare variables
integer :: i, j, k, shortest_dist
character(len=10) :: wordsDict(100000)
character(len=10) :: word1, word2
! Initialize variables
shortest_dist = 0
! Loop through the array of strings
do i = 1, size(wordsDict)
! Check if the current string is equal to word1
if (wordsDict(i) == word1) then
! Set the starting index of word1
j = i
end if
! Check if the current string is equal to word2
if (wordsDict(i) == word2) then
! Set the ending index of word2
k = i
end if
end do
! Check if both word1 and word2 were found in the array
if (j /= 0 .and. k /= 0) then
! Calculate the shortest distance between word1 and word2
shortest_dist = abs(j - k)
end if
end function shortest_distance
end module
program main
use shortest_distance
implicit none
! Declare variables
integer :: i, j, k
character(len=10) :: wordsDict(100000)
character(len=10) :: word1, word2
integer :: shortest_dist
! Initialize variables
i = 1
j = 1
k = 1
! Read the input
read (*, *) wordsDict
read (*, *) word1
read (*, *) word2
! Call the shortest_distance function
shortest_dist = shortest_distance(wordsDict, word1, word2)
! Print the output
write (*, *) shortest_dist
end program
temp.f95:7:26: 7 | function shortest_distance(wordsDict, word1, word2) result(shortest_dist) | 1 Error: MODULE attribute of ‘shortest_distance’ conflicts with PROCEDURE attribute at (1) temp.f95:10:33: 10 | integer :: i, j, k, shortest_dist | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:11:38: 11 | character(len=10) :: wordsDict(100000) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:33: 12 | character(len=10) :: word1, word2 | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:17: 15 | shortest_dist = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:18:25: 18 | do i = 1, size(wordsDict) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:35: 21 | if (wordsDict(i) == word1) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:13: 24 | j = i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:7: 26 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:35: 29 | if (wordsDict(i) == word2) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:32:13: 32 | k = i | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:34:7: 34 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:36:3: 36 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:39:29: 39 | if (j /= 0 .and. k /= 0) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:42:30: 42 | shortest_dist = abs(j - k) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:44:3: 44 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:46:3: 46 | end function shortest_distance | 1 Error: Expecting END MODULE statement at (1) temp.f95:52:5: 52 | use shortest_distance | 1 Fatal Error: Cannot open module file ‘shortest_distance.mod’ for reading at (1): No such file or directory compilation terminated.
def shortestDistance(wordsDict, word1, word2):
idx1, idx2, minDistance = -1, -1, float('inf')
for i, word in enumerate(wordsDict):
if word == word1:
idx1 = i
if word == word2:
idx2 = i
if idx1 != -1 and idx2 != -1:
minDistance = min(minDistance, abs(idx1 - idx2))
return minDistance
The algorithm iterates through the wordsDict
list and uses two variables idx1
and idx2
to store the indices of the occurrences of word1
and word2
. Initially, both indices are set to -1
. In each iteration, if word1
or word2
is found, the corresponding index variable will be updated to store the current index.
After each index update, we check if both indices are not -1
. If both indices are valid, the difference between the indices is computed, and minDistance
is updated with the minimum value between the previously-stored minimum distance and the new distance.
Finally, the algorithm returns the value of minDistance
, which represents the shortest distance between word1
and word2
in the wordsDict
list.
#include <vector>
#include <string>
#include <climits>
int shortestDistance(std::vector<std::string> wordsDict, std::string word1, std::string word2) {
int idx1 = -1, idx2 = -1, minDistance = INT_MAX;
for (int i = 0; i < wordsDict.size(); i++) {
if (wordsDict[i] == word1) idx1 = i;
if (wordsDict[i] == word2) idx2 = i;
if (idx1 != -1 && idx2 != -1)
minDistance = std::min(minDistance, std::abs(idx1 - idx2));
}
return minDistance;
}
The algorithm iterates through the wordsDict
list and uses two variables idx1
and idx2
to store the indices of the occurrences of word1
and word2
. Initially, both indices are set to -1
. In each iteration, if word1
or word2
is found, the corresponding index variable will be updated to store the current index.
After each index update, we check if both indices are not -1
. If both indices are valid, the difference between the indices is computed, and minDistance
is updated with the minimum value between the previously-stored minimum distance and the new distance.
Finally, the algorithm returns the value of minDistance
, which represents the shortest distance between word1
and word2
in the wordsDict
list.