当前位置:网站首页>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;
}
边栏推荐
- Progress in architecture
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- Leetcode T48:旋转图像
- 2022 game going to sea practical release strategy
- The game goes to sea and operates globally
- Excel快速合并多行数据
- Vscode common plug-ins summary
- 数据埋点的一些问题和想法
- MATLAB中tiledlayout函数使用
- DDD application and practice of domestic hotel transactions -- Code
猜你喜欢
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Excel快速合并多行数据
潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
失败率高达80%,企业数字化转型路上有哪些挑战?
MATLAB中tiledlayout函数使用
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Leetcode 61: 旋转链表
为什么图片传输要使用base64编码
随机推荐
flink sql-client.sh 使用教程
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Learn kernel 3: use GDB to track the kernel call chain
Real time data warehouse
流行框架:Glide的使用
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
Mask wearing detection based on yolov1
LiveData
Redis daily notes
Migration from go vendor project to mod project
Excel quickly merges multiple rows of data
R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
Abnormal value detection using shap value
Leetcode T49: 字母异位词分组
Data warehouse interview question preparation
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables