|
1 |
| -// 49. Group Anagrams |
2 |
| - |
3 |
| -// ["eat","tea","tan","ate","nat","bat"] |
4 |
| -// [["bat"],["nat","tan"],["ate","eat","tea"]] |
5 |
| - |
6 |
| -// [""] |
7 |
| -//[[""]] |
8 |
| - |
9 |
| -// ["a"] |
10 |
| -// [["a"]] |
11 | 1 | /**
|
12 | 2 | * @param {string[]} strs
|
13 | 3 | * @return {string[][]}
|
14 | 4 | */
|
15 | 5 | const groupAnagrams = (list) => {
|
16 |
| - // result, |
17 |
| - // shift -> for i & k=i+1 |
18 |
| - // twice isAnagram just to check |
19 |
| - // if we find one -> splice(index, 1) |
20 |
| - // -> group.push(item) // until the end of the list |
21 |
| - // until [] |
22 |
| - // let x = processWords(list, []); |
23 |
| - // console.log("x") |
24 |
| - // console.log(x) |
25 |
| - return processWords(list, []); |
26 |
| -} |
27 |
| - |
28 |
| -const areTheyAnagrams = (word1, word2) => { |
29 |
| - console.log(">>>") |
30 |
| - console.log(word1 || "0") |
31 |
| - console.log(word2) |
32 |
| - console.log("<<<") |
33 |
| - let char1 = (word1 || "0").split(""), |
34 |
| - char2 = (word2 || "0").split(""); |
35 |
| - |
36 |
| - if (undefined === word2 || (word1 && word2 && (word1.length !== word2.length))) return false; |
37 |
| - |
| 6 | + console.log("".charCodeAt(0) || 0) |
| 7 | + console.log("a".charCodeAt(0)) |
| 8 | + console.log("z".charCodeAt(0)) |
| 9 | + console.log("z".charCodeAt(0) - "a".charCodeAt(0)) |
| 10 | + console.log([0] * 26) |
| 11 | + console.log([23, 45, 1] == [23, 45, 1]) |
38 | 12 |
|
39 |
| - let map1 = createAnagramMap(char1), map2 = createAnagramMap(char2); |
40 |
| - console.log("map1:map2") |
41 |
| - console.log(map1) |
42 |
| - console.log(map2) |
43 |
| - |
44 |
| - |
45 |
| - // {“e” => 1, “a” => 1, “t” => 1} |
46 |
| - for (let i = 0; i < char1.length; i++) { |
47 |
| - console.log(">>") |
48 |
| - console.log(map1.get(char1[i]) !== map2.get(char1[i])) |
49 |
| - if (map1.get(char1[i]) !== map2.get(char1[i])) return false; |
50 |
| - } |
51 |
| - |
52 |
| - return true; |
53 |
| -} |
54 |
| - |
55 |
| -const createAnagramMap = (char) => { |
56 | 13 | let map = new Map();
|
57 |
| - |
58 |
| - for (let i = 0; i < char.length; i++) { |
59 |
| - map.set(char[i], (map.has(char[i]) ? (map.get(char[i]) + 1) : 1)) |
60 |
| - } |
61 |
| - |
62 |
| - return map; |
63 |
| -} |
64 |
| - |
65 |
| -const processWords = (list, groupList) => { |
66 |
| - if (0 === list.length) { |
67 |
| - return groupList; |
68 |
| - } |
69 |
| - |
70 |
| - console.log("check") |
71 |
| - console.log(list) |
72 |
| - // console.log(list.length) |
73 |
| - // console.log(groupList) |
74 |
| - // console.log(group) |
75 |
| - |
76 |
| - // ["eat","tea","tan","ate","nat","bat"] -> ["tea","tan","ate","nat","bat"] |
77 |
| - let curr = list.shift(), tmpList = [...list], group = []; |
78 |
| - group.push(curr); |
79 |
| - |
80 |
| - while (0 < list.length) { |
81 |
| - let next = list.shift(); |
82 |
| - console.log(`=> comparing ${curr}:${next}`) |
83 |
| - |
84 |
| - if (areTheyAnagrams(curr, next)) { |
85 |
| - console.log("they are") |
86 |
| - group.push(next); |
| 14 | + for (let i = 0; i < list.length; i++) { |
| 15 | + let char = list[i].split(""), asciiCountList = Array(26).fill(0); |
| 16 | + |
| 17 | + // will break for empty case, maybe 27 !! NOT, it just escape so it store a full [0]*26 |
| 18 | + for (let c = 0; c < char.length; c++) { |
| 19 | + console.log(`${char[c]}:char[c].charCodeAt(0):${char[c].charCodeAt(0)}, x:${25 - ("z".charCodeAt(0) - char[c].charCodeAt(0))}`) |
| 20 | + console.log(asciiCountList[25 - ("z".charCodeAt(0) - char[c].charCodeAt(0))]) |
| 21 | + asciiCountList[25 - ("z".charCodeAt(0) - char[c].charCodeAt(0))] += 1; |
87 | 22 | }
|
88 |
| - } |
89 | 23 |
|
90 |
| - console.log("=> group") |
91 |
| - console.log(group) |
92 |
| - // console.log("-> tmpList") |
93 |
| - // console.log(tmpList) |
| 24 | + console.log("asciiCountList:") |
| 25 | + console.log(asciiCountList) |
| 26 | + let key = JSON.stringify(asciiCountList); |
| 27 | + console.log("key:") |
| 28 | + console.log(key) |
94 | 29 |
|
95 |
| - // Remove from tmpList that are added into the group |
96 |
| - tmpList = tmpList.filter((elem) => { |
97 |
| - return !group.includes(elem); |
98 |
| - }) |
99 |
| - |
100 |
| - // console.log("tmpList <-") |
101 |
| - // console.log(tmpList) |
| 30 | + map.set(key, ((undefined === map.get(key)) ? [list[i]] : [...map.get(key), list[i]])) |
| 31 | + // map.set(asciiCountList, ((undefined === map.get(asciiSum)) ? [list[i]] : [...map.get(asciiSum), list[i]])) |
| 32 | + } |
102 | 33 |
|
103 |
| - // process for groupList |
104 |
| - groupList.push(group); |
105 |
| - console.log("groupList") |
106 |
| - console.log(groupList) |
| 34 | + console.log("map") |
| 35 | + console.log(map) |
| 36 | + console.log([...map.values()]) |
107 | 37 |
|
108 |
| - return processWords(tmpList, groupList); |
| 38 | + // return processWords(list, []); |
109 | 39 | }
|
110 | 40 |
|
111 | 41 | let x =
|
112 | 42 | // groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
|
113 |
| - // groupAnagrams(["", "b"]) |
114 |
| - // groupAnagrams(["", ""]) |
115 |
| - groupAnagrams(["tea", "and", "ace", "ad", "eat", "dans"]) |
| 43 | +// groupAnagrams(["", "b"]) |
| 44 | +groupAnagrams(["", ""]) |
| 45 | +// groupAnagrams(["tea", "and", "ace", "ad", "eat", "dans"]) |
116 | 46 | // groupAnagrams([""])
|
117 | 47 | // groupAnagrams(["a"])
|
118 | 48 | // groupAnagrams([])
|
119 | 49 |
|
120 | 50 | console.log(">>")
|
121 | 51 | console.log(x)
|
| 52 | + |
| 53 | +// // 49. Group Anagrams |
| 54 | +// |
| 55 | +// // ["eat","tea","tan","ate","nat","bat"] |
| 56 | +// // [["bat"],["nat","tan"],["ate","eat","tea"]] |
| 57 | +// |
| 58 | +// // [""] |
| 59 | +// //[[""]] |
| 60 | +// |
| 61 | +// // ["a"] |
| 62 | +// // [["a"]] |
| 63 | +// /** |
| 64 | +// * @param {string[]} strs |
| 65 | +// * @return {string[][]} |
| 66 | +// */ |
| 67 | +// const groupAnagrams = (list) => { |
| 68 | +// // result, |
| 69 | +// // shift -> for i & k=i+1 |
| 70 | +// // twice isAnagram just to check |
| 71 | +// // if we find one -> splice(index, 1) |
| 72 | +// // -> group.push(item) // until the end of the list |
| 73 | +// // until [] |
| 74 | +// // let x = processWords(list, []); |
| 75 | +// // console.log("x") |
| 76 | +// // console.log(x) |
| 77 | +// return processWords(list, []); |
| 78 | +// } |
| 79 | +// |
| 80 | +// const areTheyAnagrams = (word1, word2) => { |
| 81 | +// console.log(">>>") |
| 82 | +// console.log(word1 || "0") |
| 83 | +// console.log(word2) |
| 84 | +// console.log("<<<") |
| 85 | +// let char1 = (word1 || "0").split(""), |
| 86 | +// char2 = (word2 || "0").split(""); |
| 87 | +// |
| 88 | +// if (undefined === word2 || (word1 && word2 && (word1.length !== word2.length))) return false; |
| 89 | +// |
| 90 | +// |
| 91 | +// let map1 = createAnagramMap(char1), map2 = createAnagramMap(char2); |
| 92 | +// console.log("map1:map2") |
| 93 | +// console.log(map1) |
| 94 | +// console.log(map2) |
| 95 | +// |
| 96 | +// |
| 97 | +// // {“e” => 1, “a” => 1, “t” => 1} |
| 98 | +// for (let i = 0; i < char1.length; i++) { |
| 99 | +// console.log(">>") |
| 100 | +// console.log(map1.get(char1[i]) !== map2.get(char1[i])) |
| 101 | +// if (map1.get(char1[i]) !== map2.get(char1[i])) return false; |
| 102 | +// } |
| 103 | +// |
| 104 | +// return true; |
| 105 | +// } |
| 106 | +// |
| 107 | +// const createAnagramMap = (char) => { |
| 108 | +// let map = new Map(); |
| 109 | +// |
| 110 | +// for (let i = 0; i < char.length; i++) { |
| 111 | +// map.set(char[i], (map.has(char[i]) ? (map.get(char[i]) + 1) : 1)) |
| 112 | +// } |
| 113 | +// |
| 114 | +// return map; |
| 115 | +// } |
| 116 | +// |
| 117 | +// const processWords = (list, groupList) => { |
| 118 | +// if (0 === list.length) { |
| 119 | +// return groupList; |
| 120 | +// } |
| 121 | +// |
| 122 | +// console.log("check") |
| 123 | +// console.log(list) |
| 124 | +// // console.log(list.length) |
| 125 | +// // console.log(groupList) |
| 126 | +// // console.log(group) |
| 127 | +// |
| 128 | +// // ["eat","tea","tan","ate","nat","bat"] -> ["tea","tan","ate","nat","bat"] |
| 129 | +// let curr = list.shift(), tmpList = [...list], group = []; |
| 130 | +// group.push(curr); |
| 131 | +// |
| 132 | +// while (0 < list.length) { |
| 133 | +// let next = list.shift(); |
| 134 | +// console.log(`=> comparing ${curr}:${next}`) |
| 135 | +// |
| 136 | +// if (areTheyAnagrams(curr, next)) { |
| 137 | +// console.log("they are") |
| 138 | +// group.push(next); |
| 139 | +// } |
| 140 | +// } |
| 141 | +// |
| 142 | +// console.log("=> group") |
| 143 | +// console.log(group) |
| 144 | +// // console.log("-> tmpList") |
| 145 | +// // console.log(tmpList) |
| 146 | +// |
| 147 | +// // Remove from tmpList that are added into the group |
| 148 | +// tmpList = tmpList.filter((elem) => { |
| 149 | +// return !group.includes(elem); |
| 150 | +// }) |
| 151 | +// |
| 152 | +// // console.log("tmpList <-") |
| 153 | +// // console.log(tmpList) |
| 154 | +// |
| 155 | +// // process for groupList |
| 156 | +// groupList.push(group); |
| 157 | +// console.log("groupList") |
| 158 | +// console.log(groupList) |
| 159 | +// |
| 160 | +// return processWords(tmpList, groupList); |
| 161 | +// } |
| 162 | +// |
| 163 | +// let x = |
| 164 | +// // groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]) |
| 165 | +// // groupAnagrams(["", "b"]) |
| 166 | +// // groupAnagrams(["", ""]) |
| 167 | +// groupAnagrams(["tea", "and", "ace", "ad", "eat", "dans"]) |
| 168 | +// // groupAnagrams([""]) |
| 169 | +// // groupAnagrams(["a"]) |
| 170 | +// // groupAnagrams([]) |
| 171 | +// |
| 172 | +// console.log(">>") |
| 173 | +// console.log(x) |
0 commit comments