当前位置:网站首页>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;
}
边栏推荐
- 数据埋点的一些问题和想法
- Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
- Real time data warehouse
- QT how to detect whether the mouse is on a control
- What is the real meaning and purpose of doing things, and what do you really want
- 统计php程序运行时间及设置PHP最长运行时间
- ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
- GCC [6] - 4 stages of compilation
- The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
- LiveData
猜你喜欢
Data center concept
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
sql优化之查询优化器
Oppo find N2 product form first exposure: supplement all short boards
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
Use of tiledlayout function in MATLAB
NowCoder 反转链表
Product identification of intelligent retail cabinet based on paddlex
Test process arrangement (3)
Visual Studio调试方式详解
随机推荐
gin集成支付宝支付
Mongodb commonly used 28 query statements (forward)
ViewModel 初体验
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
Error in find command: paths must precede expression (turn)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
vscode 常用插件汇总
学内核之三:使用GDB跟踪内核调用链
第十六章 字符串本地化和消息字典(二)
Excel快速合并多行数据
ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
Test evaluation of software testing
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
Why should Base64 encoding be used for image transmission
The game goes to sea and operates globally
nowcoder重排链表
Progress in architecture
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own