当前位置:网站首页>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 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
- Detailed index of MySQL
- 【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
- Popular framework: the use of glide
- 第十六章 字符串本地化和消息字典(二)
- The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
- 数据埋点的一些问题和想法
- R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
- 【云原生】我怎么会和这个数据库杠上了?
- 迅为IMX6Q开发板QT系统移植tinyplay
猜你喜欢
递增的三元子序列[贪心训练]
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
实战解惑 | OpenCV中如何提取不规则ROI区域
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value
Test process arrangement (3)
Leetcode 61: 旋转链表
随机推荐
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
Innovation and development of independent industrial software
Error in find command: paths must precede expression (turn)
92.(cesium篇)cesium楼栋分层
Code hoof collection of wonderful secret place
Abnormal value detection using shap value
Test evaluation of software testing
Redis daily notes
流行框架:Glide的使用
第十七章 进程内存
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Visual Studio调试方式详解
游戏出海,全球化运营
sql优化之explain
尊重他人的行为
File creation, writing, reading, deletion (transfer) in go language
【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
一文概览2D人体姿态估计
测试流程整理(2)