[算法练习] - [数组] - 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"])
相关推荐
神仙别闹10 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
伞伞悦读11 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
有点。11 小时前
C++03阶段练习(练习题)
数据结构·算法·图论
GreenTea11 小时前
GrokBot 核心成员 Lauren Tan:每月交付 2000 个 PR 的人,是怎么用 AI 的
前端·后端·架构
周末也要写八哥12 小时前
经典算法实例:游戏中弱角色的数量(二)
算法
mCell12 小时前
用 Knip 清理 AI Coding 留下的冗余代码
前端·ai编程·前端工程化
是Yu欸12 小时前
鸿蒙PC移植:2048 从网页小游戏到 AI 桌面应用
大数据·人工智能·算法·数据挖掘·openharmony·codex
鹿角片ljp12 小时前
KV Cache 解析
java·算法
liliangcsdn13 小时前
IVOL与偏度因子的对比测量分析
算法
笃行35013 小时前
使用Rokid AIUI做了一个童年推箱子游戏
前端