Consider I have this array:
$array = array(
'word1',
'abc',
'abc',
'word2',
[other words]
'word1',
'dfg'
'word2',
[other words]
);
I need to find the minimum distance between 2 given words. (let ‘word1’ and ‘word2’ be these 2 words)
In this case the minium distance between word1 and word2 is 1 because in the second group of words they are separated by only ‘dfg’.
I wrote a simple code but it’s too expsensive and I am looking for a faster version.
//> PSEUDO CODE
function minDistance( $words, $word1, $word2 ) {
foreach( $words as $k=>$v)
if ( $v == $words1 )
$positionsOfFirstWord[] = $k;
if ( $v == $words2 )
$positionsOfSecondWord[] = $k;
//> If word1 or word2 was not found in the array then
//> return max distance possibile (count($words))
//> Now we have 2 array containg the position of both word we need.
foreach( $positionsOfFirstWord as $v )
foreach( $positionsOfSecondWord as $vv )
$distance = abs($vv-$v);
}
Note the order of words in $array isn’t important (that’s why there is abs())
Do you think there could be a better version?
Please note the function must return 1 in this case too:
array(
[other words]
'word2',
'dfg',
'word1'
[other words]
);
I think a simple loop is enough. Keep track over the current minimum and of the
last word1and update current minimum if aword2is found. Basically you are utilizing the fact that aword2will always be closest to the lastword1foundYou can do this in
O(n)but there might faster ways if pre processing can be done and you need to answer multiple queries