243.最短单词距离
小于 1 分钟
243.最短单词距离
/*
* @lc app=leetcode.cn id=228 lang=typescript
*
* [243] 最短单词距离
*/
// @lc code=start
function shortestDistance(
wordsDict: string[],
word1: string,
word2: string
): number {
let firstIndex: number[] = [];
let secondIndex: number[] = [];
wordsDict.map((el, index) => {
if (el == word1) firstIndex.push(index);
if (el == word2) secondIndex.push(index);
});
let nums: number[] = [];
firstIndex.map(el => {
secondIndex.map(cl => {
nums.push(Math.abs(el - cl));
});
});
return Math.min(...nums);
}
// @lc code=end
Loading...
