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