leetcode学习笔记49 Group Anagrams

这篇博客介绍了LeetCode中的49题——将字母异位词分组。文章通过示例解释了问题,并提出一种时间复杂度为O(mn)、空间复杂度为O(mn)的解决方案,其中m为字符串数量,n为最长字符串的长度。主要思路是利用字符计数并转化为字符串作为Map键来存储原始字符串。
摘要由CSDN通过智能技术生成

leetcode学习笔记49

问题

Group Anagrams

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: strs = [“eat”,“tea”,“tan”,“ate”,“nat”,“bat”]
Output: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]

Example 2:

Input: strs = [""]
Output: [[""]]

Example 3:

Input: strs = [“a”]
Output: [[“a”]]

Constraints:

  • 1 <= strs.length <= 104
  • 0 <= strs[i].length <= 100
  • strs[i] consists of lower-case English letters.

思考

只含有小写英文字符,所以只需要size为26的数组就可以统计出现的字符的个数, 然后按照顺序转换为String, 将String 作为map的key 来储存原始字符串. 最后将map的values 输出为List.

方法1

时间复杂度O(mn).
空间复杂度O(mn).

m: 字符串的个数
n:最长字符串的长度

class Solution 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值