JSFiddle - React, Tailwind, and code Playground

by nwali

JavaScript

var solution = function(words){
	let graph = this.buildGraph(words);
}

var buildGraph = function(words) {
console.log("called")
	let adList = {};
  
  for (var i=0; i<words.length-1; i++){
  	let w1 = words[i],
    		w2 = words[i+1],
        j  = 0;
    
    while(j < Math.min(w1.length, w2.length)) {
    	if (w1[j] !== w2[j]) {
      	if (!adList[w1[j]]) { adList[w1[j]] = new Set(); }
        	console.log(w1[j]);
	        adList[w1[j]].add(w2[j]);
          break;
      }
      j++;
    }
  }
	console.log(adList);
	return adList;
}

console.log(solution([ "wrt","wrf","er","ett","rftt"]));