[算法练习] - [数组] - js实现字母异位词分组

js实现字母异位词分组

输入: strs = "eat", "tea", "tan", "ate", "nat", "bat"

输出: \["bat","nat","tan","ate","eat","tea"]

js 复制代码
function groupAnagrams(strs) {
	const map = new Map();
	for (const str of strs) {
		const sortedStr = str.split('').sort().join('');
		console.log(sortedStr, 'sortedStr')
		if (!map.has(sortedStr)) {
			map.set(sortedStr, []);
		}
		map.get(sortedStr).push(str);
	}
	return Array.from(map.values());
}
const arr = groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])
相关推荐
bkspiderx5 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆6 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光6 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
信奥卷王6 小时前
[GESP202609 六级] 分树规划
算法
夏幻灵6 小时前
LeetCode 21:合并两个有序链表——双指针与哑节点详解
算法·leetcode·链表
明月_清风6 小时前
Deno 终局来了:从挑战 Node 到被 Cloudflare 收编
前端·后端·node.js
码云数智-园园6 小时前
unique_ptr 还是 shared_ptr?C++ 智能指针选型与内存泄漏实战分析
java·开发语言
禾小西7 小时前
07丨Redis 哨兵机制:主库故障后,如何恢复服务?
java·开发语言·redis
我不是阵雨7 小时前
JDK 21虚拟线程Pinning陷阱:一文拔钉解困
java·开发语言