Skip to content

Commit 347971e

Browse files
committed
❌ [49] TLE
1 parent 2b714ff commit 347971e

File tree

2 files changed

+140
-16
lines changed

2 files changed

+140
-16
lines changed

49/my_solution.js

+71-16
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,45 @@
88

99
// ["a"]
1010
// [["a"]]
11-
11+
/**
12+
* @param {string[]} strs
13+
* @return {string[][]}
14+
*/
1215
const groupAnagrams = (list) => {
1316
// result,
1417
// shift -> for i & k=i+1
1518
// twice isAnagram just to check
1619
// if we find one -> splice(index, 1)
1720
// -> group.push(item) // until the end of the list
1821
// until []
19-
processWords(list, [], []);
22+
// let x = processWords(list, []);
23+
// console.log("x")
24+
// console.log(x)
25+
return processWords(list, []);
2026
}
2127

2228
const areTheyAnagrams = (word1, word2) => {
23-
if (undefined === word2) return false;
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+
38+
39+
let map1 = createAnagramMap(char1), map2 = createAnagramMap(char2);
40+
console.log("map1:map2")
41+
console.log(map1)
42+
console.log(map2)
43+
2444

25-
let char = word1.split(""), map1 = createAnagramMap(char), map2 = createAnagramMap(word2.split(""));
2645
// {“e” => 1, “a” => 1, “t” => 1}
27-
for (let i = 0; i < char.length; i++) {
28-
if (map1.get(char[i]) !== map2.get(char[i])) return false;
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;
2950
}
3051

3152
return true;
@@ -41,26 +62,60 @@ const createAnagramMap = (char) => {
4162
return map;
4263
}
4364

44-
const processWords = (list, groupList, group) => {
45-
if ([] === list) {
65+
const processWords = (list, groupList) => {
66+
if (0 === list.length) {
4667
return groupList;
4768
}
4869

49-
let curr = list.shift(); // ["eat","tea","tan","ate","nat","bat"] -> ["tea","tan","ate","nat","bat"]
50-
let tmpList = list;
70+
console.log("check")
71+
console.log(list)
72+
// console.log(list.length)
73+
// console.log(groupList)
74+
// console.log(group)
5175

52-
while ([] !== list) {
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) {
5381
let next = list.shift();
54-
if (areTheyAnagrams(curr, next)) group.push(next);
82+
console.log(`=> comparing ${curr}:${next}`)
83+
84+
if (areTheyAnagrams(curr, next)) {
85+
console.log("they are")
86+
group.push(next);
87+
}
5588
}
5689

57-
// remove from tmpList that are added into the group
58-
tmpList.filter((item) => {
59-
return !group.includes(item);
90+
console.log("=> group")
91+
console.log(group)
92+
// console.log("-> tmpList")
93+
// console.log(tmpList)
94+
95+
// Remove from tmpList that are added into the group
96+
tmpList = tmpList.filter((elem) => {
97+
return !group.includes(elem);
6098
})
6199

100+
// console.log("tmpList <-")
101+
// console.log(tmpList)
102+
62103
// process for groupList
63104
groupList.push(group);
105+
console.log("groupList")
106+
console.log(groupList)
64107

65-
return processWords(tmpList, groupList, group);
108+
return processWords(tmpList, groupList);
66109
}
110+
111+
let x =
112+
// groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
113+
// groupAnagrams(["", "b"])
114+
// groupAnagrams(["", ""])
115+
groupAnagrams(["tea", "and", "ace", "ad", "eat", "dans"])
116+
// groupAnagrams([""])
117+
// groupAnagrams(["a"])
118+
// groupAnagrams([])
119+
120+
console.log(">>")
121+
console.log(x)

49/solution.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
const groupAnagrams = (list) => {
6+
// result,
7+
// shift -> for i & k=i+1
8+
// twice isAnagram just to check
9+
// if we find one -> splice(index, 1)
10+
// -> group.push(item) // until the end of the list
11+
// until []
12+
// let x = processWords(list, []);
13+
// console.log("x")
14+
// console.log(x)
15+
return processWords(list, []);
16+
}
17+
18+
const areTheyAnagrams = (word1, word2) => {
19+
let char1 = (word1 || "0").split(""),
20+
char2 = (word2 || "0").split("");
21+
22+
if (undefined === word2 || (word1 && word2 && (word1.length !== word2.length))) return false;
23+
24+
25+
let map1 = createAnagramMap(char1), map2 = createAnagramMap(char2);
26+
27+
for (let i = 0; i < char1.length; i++) {
28+
if (map1.get(char1[i]) !== map2.get(char1[i])) return false;
29+
}
30+
31+
return true;
32+
}
33+
34+
const createAnagramMap = (char) => {
35+
let map = new Map();
36+
37+
for (let i = 0; i < char.length; i++) {
38+
map.set(char[i], (map.has(char[i]) ? (map.get(char[i]) + 1) : 1))
39+
}
40+
41+
return map;
42+
}
43+
44+
const processWords = (list, groupList) => {
45+
if (0 === list.length) {
46+
return groupList;
47+
}
48+
49+
let curr = list.shift(), tmpList = [...list], group = [];
50+
group.push(curr);
51+
52+
while (0 < list.length) {
53+
let next = list.shift();
54+
55+
if (areTheyAnagrams(curr, next)) {
56+
group.push(next);
57+
}
58+
}
59+
60+
// Remove from tmpList that are added into the group
61+
tmpList = tmpList.filter((elem) => {
62+
return !group.includes(elem);
63+
})
64+
65+
// process for groupList
66+
groupList.push(group);
67+
68+
return processWords(tmpList, groupList);
69+
}

0 commit comments

Comments
 (0)