You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Design a data structure that will be initialized with a string array, and then it should answer queries of the shortest distance between two different strings from the array.
Implement the WordDistance class:
WordDistance(String[] wordsDict) initializes the object with the strings array wordsDict.
int shortest(String word1, String word2) returns the shortest distance between word1 and word2 in the array wordsDict.
/** * @param {string[]} wordsDict *//* wordsDict: ['practice', 'makes', 'perfect', 'coding', 'makes'] map: {practice: [0], makes: [1, 4], perfect: [2], coding: [3]} */varWordDistance=function(wordsDict){letmap={};for(leti=0;i<wordsDict.length;i++){if(map[wordsDict[i]]==null)map[wordsDict[i]]=[];map[wordsDict[i]].push(i);}this.map=map;};/** * @param {string} word1 * @param {string} word2 * @return {number} */WordDistance.prototype.shortest=function(word1,word2){letw1=this.map[word1];letw2=this.map[word2];letminDist=Infinity;letw1Index=0,w2Index=0;while(w1Index<w1.length&&w2Index<w2.length){minDist=Math.min(minDist,Math.abs(w1[w1Index]-w2[w2Index]));if(w1[w1Index]<w2[w2Index])w1Index++;elsew2Index++;}returnminDist};/** * Your WordDistance object will be instantiated and called as such: * var obj = new WordDistance(wordsDict) * var param_1 = obj.shortest(word1,word2) */