|
| 1 | + |
| 2 | +/** |
| 3 | + * Add nodes and edges into a graph using adjacency list |
| 4 | + * |
| 5 | + * @class Graph |
| 6 | + */ |
| 7 | +classGraph{ |
| 8 | +constructor(){ |
| 9 | +this.nodes=newMap(); |
| 10 | +} |
| 11 | + |
| 12 | +// add node or directed edge |
| 13 | +add(node1,node2){ |
| 14 | +// console.log({node1, node2}) |
| 15 | +constadj=this.nodes.has(node1) ? this.nodes.get(node1) : newSet(); |
| 16 | +if(node2)adj.add(node2); |
| 17 | +this.nodes.set(node1,adj); |
| 18 | +} |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * DFS + tarjan for loop detection |
| 23 | + * |
| 24 | + * @param {Graph} g |
| 25 | + * @param {Map} node |
| 26 | + * @param {Set} set |
| 27 | + * @param {Map} [parent=null] |
| 28 | + * @param {Map} [grouping=new Map()] |
| 29 | + * @param {number} [depth=0] |
| 30 | + * @returns {boolean} true if has a loop, false otherwise |
| 31 | + */ |
| 32 | +functionhasLoopOrAddToSet(g,node,set,parent=null,grouping=newMap(),depth=0){ |
| 33 | +if(set.has(node))set.delete(node); |
| 34 | +set.add(node); |
| 35 | +grouping.set(node,depth); |
| 36 | + |
| 37 | +// console.log({node, adjs: g.nodes.get(node)}); |
| 38 | + |
| 39 | +for(constadjofg.nodes.get(node)){ |
| 40 | +// if (adj === parent) continue; // only for indirected graph |
| 41 | + |
| 42 | +if(!grouping.has(adj)){ |
| 43 | +if(hasLoopOrAddToSet(g,adj,set,node,grouping,depth+1))returntrue; |
| 44 | +} |
| 45 | + |
| 46 | +constminGroup=Math.min(grouping.get(adj),grouping.get(node)); |
| 47 | +grouping.set(node,minGroup); |
| 48 | + |
| 49 | +if(grouping.get(adj)===grouping.get(node))returntrue; |
| 50 | +} |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +/** |
| 55 | + * Find the order of the alien alphabet given a list of words on lexicographical order. |
| 56 | + * |
| 57 | + * @param {string[]} words |
| 58 | + * @return {string} The alien alphabet order. |
| 59 | + */ |
| 60 | +functionalienOrder(words){ |
| 61 | +constg=newGraph(); |
| 62 | +if(!words||words.length<2)returnwords&&words.join(''); |
| 63 | + |
| 64 | +for(leti=1;i<words.length;i++){// O(n) * O(k) |
| 65 | +constw1=words[i-1]; |
| 66 | +constw2=words[i]; |
| 67 | +letj=0; |
| 68 | + |
| 69 | +while(j<w1.length&&j<w2.length&&w1[j]===w2[j]){// O(k), k = max word length |
| 70 | +g.add(w1[j++]); |
| 71 | +} |
| 72 | + |
| 73 | +if(j===w2.length&&w1.length>w2.length){ |
| 74 | +return'';// shorter words should come first. |
| 75 | +} |
| 76 | + |
| 77 | +if(w1[j])g.add(w1[j],w2[j]); |
| 78 | +[...w1.slice(j)].forEach((n)=>g.add(n)); |
| 79 | +[...w2.slice(j)].forEach((n)=>g.add(n)); |
| 80 | +} |
| 81 | + |
| 82 | +// console.log({ g: JSON.stringify(g) }); |
| 83 | +// console.log({ g: g.nodes }); |
| 84 | + |
| 85 | +constset=newSet(); |
| 86 | +for(const[node]ofg.nodes){// O(?) |
| 87 | +if(hasLoopOrAddToSet(g,node,set)){// DFS: O(E + V), V: total unique letters |
| 88 | +return''; |
| 89 | +} |
| 90 | +} |
| 91 | + |
| 92 | +return[...set].join(''); |
| 93 | +} |
| 94 | + |
| 95 | +module.exports=alienOrder; |
| 96 | + |
| 97 | +// Find the order of the alien alphabet given a list of words on lexicographical order. |
| 98 | + |
| 99 | +// take words in pair, and build a dependency graph. |
| 100 | +// skip while w1.char === w2.char |
| 101 | +// add the first diff chars as a adj nodes |
| 102 | +// add each letter as a node in the graph. |
| 103 | + |
| 104 | +// find the order of the alien alph |
| 105 | +// iterate over each node |
| 106 | +// dfs + tarjan to detect loops |
| 107 | +// add each visited node to a set |
| 108 | +// if there’s a loop return ‘’ |
| 109 | +// return set |
0 commit comments