Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions distance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* A problem collected from Voz =))
*
* Given an array of strings, write a function return the minimum distance between a and b in array
*/

function solution(list = [], a = '', b = '') {
if (list.length === 0) return -1;
if (a.length === 0 || b.length === 0) return -1;

let firstIdx = -1, secondIdx = -1;
let minDist = list.length;

for (let i = 0; i < list.length; ++i) {
if (list[i] === a) {
firstIdx = i
minDist = Math.min(Math.abs(firstIdx - secondIdx), minDist);
} else if (list[i] === b) {
secondIdx = i
minDist = Math.min(Math.abs(firstIdx - secondIdx), minDist);
}
}

return minDist;
}

console.log(solution(["cat", "dog", "bird", "fish", "cat","duck","chicken","dog"], 'dog', 'duck'));
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading