-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathrobinyoon-dev.js
More file actions
45 lines (33 loc) Β· 1.13 KB
/
robinyoon-dev.js
File metadata and controls
45 lines (33 loc) Β· 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @param {string[]} strs
* @return {string[][]}
*/
// λ¬Έμ λ νμμΌλ μκ° λ³΅μ‘λ μΈ‘λ©΄μμ ν¨μ¨μ΄ λ무 λ¨μ΄μ§λ νμ΄ λ°©λ²....
var groupAnagrams = function (strs) {
let outputArr = [];
let countArr = [];
const A_ASCII = 'a'.charCodeAt(0);
const Z_ASCII = 'z'.charCodeAt(0);
let charCounts = Z_ASCII - A_ASCII + 1;
let charCountArr = new Array(charCounts).fill(0); //μΈλ±μ€κ° μνλ²³μ λνλ.
for (str of strs) {
let strCountString = getStrCountString(str);
let hasSameCountIndex = countArr.findIndex((item) => item === strCountString);
if (hasSameCountIndex !== -1) {
outputArr[hasSameCountIndex].push(str);
} else {
countArr.push(strCountString);
outputArr.push([str]);
}
}
return outputArr;
function getStrCountString(str) {
let tempArr = [...charCountArr];
for (char of str) {
let charAscii = char.charCodeAt(0);
let charIndex = charAscii - A_ASCII;
tempArr[charIndex] += 1;
}
return tempArr.join(',');
}
};