当前位置:网站首页>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;
}
边栏推荐
- docker-compose公网部署redis哨兵模式
- 迅为IMX6Q开发板QT系统移植tinyplay
- Excel快速合并多行数据
- Common content type correspondence table
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- 如何游戏出海代运营、游戏出海代投
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
- TestSuite and testrunner in unittest
- GCC [6] - 4 stages of compilation
猜你喜欢
数据湖(十三):Spark与Iceberg整合DDL操作
DDD application and practice of domestic hotel transactions -- Code
MATLAB中tiledlayout函数使用
测试流程整理(3)
一文概览2D人体姿态估计
实时数据仓库
Visual Studio调试方式详解
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
Test process arrangement (2)
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
随机推荐
Test evaluation of software testing
gin集成支付宝支付
按照功能对Boost库进行分类
海外游戏代投需要注意的
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
Abnormal value detection using shap value
Error in find command: paths must precede expression (turn)
Common content type correspondence table
PHP log debugging
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用stress.col参数指定强调线的id子集的颜色(色彩)
卷积神经网络经典论文集合(深度学习分类篇)
Install and use MAC redis, connect to remote server redis
Innovation and development of independent industrial software
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
Understand chisel language thoroughly 04. Chisel Foundation (I) - signal type and constant
使用CLion编译OGLPG-9th-Edition源码
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
LiveData