MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / canBuildGraph

Function canBuildGraph

javascript/0269-alien-dictionary.js:38–62  ·  view source on GitHub ↗
(words, graph, frequencyMap)

Source from the content-addressed store, hash-verified

36};
37
38var canBuildGraph = (words, graph, frequencyMap) => {
39 for (let index = 0; index < words.length - 1; index++) {
40 const [word1, word2] = [words[index], words[index + 1]];
41 const minLength = Math.min(word1.length, word2.length);
42
43 const isWord1Longer = word2.length < word1.length;
44 const isPrefix = isWord1Longer && word1.startsWith(word2);
45
46 if (isPrefix) return false;
47
48 for (let j = 0; j < minLength; j++) {
49 const [char1, char2] = [word1[j], word2[j]];
50
51 const isEqual = char1 === char2;
52 if (isEqual) continue;
53
54 graph.get(char1).push(char2);
55 frequencyMap.set(char2, frequencyMap.get(char2) + 1);
56
57 break;
58 }
59 }
60
61 return true;
62};
63
64const bfs = (queue, frequencyMap, graph, buffer) => {
65 while (!queue.isEmpty()) {

Callers 1

alienOrderFunction · 0.85

Calls 4

startsWithMethod · 0.45
pushMethod · 0.45
getMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected