当前位置:网站首页>Leetcode t49: grouping of alphabetic words
Leetcode t49: grouping of alphabetic words
2022-07-04 14:24:00 【Fan Qianzhi】
Title Description
Here's an array of strings , Would you please Letter heterotopic word Put together . You can return a list of results in any order .
Letter heterotopic word Is a new word obtained by rearranging the letters of the source word , Letters in all source words are usually used just 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”]]
Tips :
- 1 <= strs.length <= 104
- 0 <= strs[i].length <= 100
- strs[i] Only lowercase letters
Ideas
If two strings are sorted according to characters , The resulting strings are equal , Then these two strings are alphabetic ectopic words .
Use HashMap, Improve access efficiency .
Code
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
public List<List<String>> groupAnagrams(String[] strs) {
for(int i = 0; i < strs.length; i++) {
char[] chs = strs[i].toCharArray();
Arrays.sort(chs);
if(map.containsKey(new String(chs))) {
map.get(new String(chs)).add(i);
} else {
List<Integer> lis = new ArrayList<Integer>();
lis.add(i);
map.put(new String(chs), lis);
}
}
List<List<String>> res = new ArrayList<List<String>>();
for(List<Integer> lis: map.values()) {
List<String> tmp = new ArrayList<String>();
for(int ind: lis) tmp.add(strs[ind]);
res.add(tmp);
}
return res;
}
边栏推荐
- Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
- Install and use MAC redis, connect to remote server redis
- LifeCycle
- Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
- sharding key type not supported
- opencv3.2 和opencv2.4安装
- 数据埋点的一些问题和想法
- One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
- Blob, text geometry or JSON column'xxx'can't have a default value query question
- PyTorch的自动求导机制详细解析,PyTorch的核心魔法
猜你喜欢

Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value

【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程

flink sql-client.sh 使用教程

docker-compose公网部署redis哨兵模式

Unity shader learning (3) try to draw a circle

Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators

【信息检索】链接分析

MATLAB中tiledlayout函数使用

leetcode:6110. 网格图中递增路径的数目【dfs + cache】
![[FAQ] summary of common causes and solutions of Huawei account service error 907135701](/img/43/1a9786c89a5ab21d1fb8903cb7b77e.png)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
随机推荐
MySQL的存储过程练习题
Vscode common plug-ins summary
Gorm data insertion (transfer)
实战解惑 | OpenCV中如何提取不规则ROI区域
LifeCycle
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
如何游戏出海代运营、游戏出海代投
Leetcode T47: 全排列II
Introducing testfixture into unittest framework
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
Excel快速合并多行数据
商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
Gorm read / write separation (rotation)
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
C # WPF realizes the real-time screen capture function of screen capture box
Why should Base64 encoding be used for image transmission
TestSuite and testrunner in unittest
利用Shap值进行异常值检测
流行框架:Glide的使用