[算法练习] - [数组] - 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"])
相关推荐
Simon_He3 分钟前
一个免费的在线压缩网站超越了付费的压缩软件
前端·开源·图片资源
喝可乐的布偶猫9 分钟前
韩顺平之第九章综合练习-----------房屋出租管理系统
java·开发语言·ide·eclipse
江山如画,佳人北望28 分钟前
C#程序入门
开发语言·windows·c#
巴巴_羊38 分钟前
React Ref使用
前端·javascript·react.js
拾光拾趣录1 小时前
CSS常见问题深度解析与解决方案(第三波)
前端·css
徊忆羽菲1 小时前
Echarts3D柱状图-圆柱体-文字在柱体上垂直显示的实现方法
javascript·ecmascript·echarts
轻语呢喃1 小时前
JavaScript :字符串模板——优雅编程的基石
前端·javascript·后端
杨进军1 小时前
React 协调器 render 阶段
前端·react.js·前端框架
中微子1 小时前
Blob 对象及 Base64 转换指南
前端
风铃喵游1 小时前
让大模型调用MCP服务变得超级简单
前端·人工智能