Skip to content

Commit cf478e1

Browse files
committed
add 49 js solution
1 parent 8cefb8d commit cf478e1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@
395395
|52|[N-Queens II](https://leetcode.com/problems/n-queens-ii/)| |Hard|
396396
|51|[N-Queens](https://leetcode.com/problems/n-queens/)| |Hard|
397397
|50|["Pow(x, n)"](https://leetcode.com/problems/powx-n/)| |Medium|
398-
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| |Medium|
398+
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/)| [js](./algorithms/groupAnagrams/solution.js) |Medium|
399399
|48|[Rotate Image](https://leetcode.com/problems/rotate-image/)| |Medium|
400400
|47|[Permutations II](https://leetcode.com/problems/permutations-ii/)| |Hard|
401401
|46|[Permutations](https://leetcode.com/problems/permutations/)| [js](./algorithms/permutations/Solution.js) |Medium|

algorithms/groupAnagrams/solution.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function (strs) {
6+
const groupMap = new Map()
7+
for (let i = 0; i < strs.length; i++) {
8+
console.log(`${i}: 当前处理的字符串:${strs[i]}`)
9+
// 查找当前字符串与另外一个字符串是否是字母异位词
10+
let exist = false;
11+
for (let [key, { group, chatMap }] of groupMap) {
12+
console.log('groupMap key: ', key)
13+
// 先简单判断长度,不符合直接跳出此次循环
14+
if (strs[i].length !== key.length) {
15+
console.log('长度不一致继续循环')
16+
continue
17+
}
18+
const _chatMap = new Map(chatMap)
19+
for (let j = 0; j < strs[i].length; j++) {
20+
const chat = strs[i][j];
21+
console.log(`当前查找的字符=>${chat}`)
22+
const findChat = _chatMap.has(chat)
23+
console.log('findChat:', findChat)
24+
if (findChat) {
25+
// 找到了,就把 value - 1
26+
_chatMap.set(chat, _chatMap.get(chat) - 1)
27+
continue
28+
}
29+
// 找不到则表面不符合,直接跳出循环
30+
console.log('不符合条件跳出循环')
31+
break
32+
}
33+
// 判断当前chatMap是否都已经被匹配完
34+
if (Array.from(_chatMap.values()).every(item => item === 0)) {
35+
console.warn(`===${strs[i]}存在字母异位词${key}===`)
36+
// 插入到匹配的group中
37+
group.push(strs[i])
38+
exist = true
39+
break
40+
}
41+
}
42+
43+
if (!exist) {
44+
// 最后没有匹配的自己成立一组
45+
// 创建映射表,value 是各个字符出现次数的映射表 chatMap 和 最终要返回的分组 group
46+
// 各个字符出现次数的映射表 chatMap 举例:aabbc => {a => 2, b => 2, c => 1}
47+
const chatMap = new Map()
48+
strs[i].split('').forEach(s => {
49+
chatMap.set(s, chatMap.has(s) ? chatMap.get(s) + 1 : 1)
50+
})
51+
groupMap.set(strs[i], {
52+
group: [strs[i]],
53+
chatMap
54+
})
55+
}
56+
57+
}
58+
return Array.from(groupMap.values()).map(item => item.group);
59+
};
60+
61+
62+
// 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
63+
// let strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
64+
// 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
65+
66+
// let strs = ["ddddddddddg","dgggggggggg"]
67+
68+
console.log(groupAnagrams(strs))

0 commit comments

Comments
 (0)