Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion javascript/49-Group-Anagrams.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@
// This solution is faster than sorting each word.
//////////////////////////////////////////////////////////////////////////////

/** @const {!Object<string, number>} */
const CODES = {
a: 0,
b: 1,
c: 2,
d: 3,
e: 4,
f: 5,
g: 6,
h: 7,
i: 8,
j: 9,
k: 10,
l: 11,
m: 12,
n: 13,
o: 14,
p: 15,
q: 16,
r: 17,
s: 18,
t: 19,
u: 20,
v: 21,
w: 22,
x: 23,
y: 24,
z: 25,
};

/**
* @param {string[]} words
* @return {string[][]}
Expand Down Expand Up @@ -33,7 +63,7 @@ function groupAnagrams(words) {
function hashWord(word) {
const hash = new Array(26).fill(0);
for (const ch of word) {
++hash[ch.charCodeAt(0) - 'a'.charCodeAt(0)];
++hash[CODES[ch]];
}
return hash.toString();
}
Expand Down