当前位置:网站首页>49. Grouping of alphabetic ectopic words: give you a string array, please combine the alphabetic ectopic words together. You can return a list of results in any order. An alphabetic ectopic word is a
49. Grouping of alphabetic ectopic words: give you a string array, please combine the alphabetic ectopic words together. You can return a list of results in any order. An alphabetic ectopic word is a
2022-07-05 13:21:00 【? abc!】
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”]]
Ideas
From the meaning of the topic :
- The two strings are mutually alphabetic ectopic words , need
The two strings contain the same letters
- So right.
The two strings are sorted separately
The resulting string must be the same - Use a hash table to store each set of alphabetic ectopic words ,
The key of the hash table is the symbol of a group of letter ectopic words , The value of the hash table is a list of alphabetic ectopic words
Take the sorted string as the key of the hash table
Code
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
// The hash table : The sorted string is used as the key of the hash table , The value is a list of alphabetic ectopic words
Map<String,List<String>> map = new HashMap<String,List<String>>();
// Create loop , Traverse each string
for(String str : strs){
// Split the string into an array of characters
char[] array = str.toCharArray();
// Sort the array
Arrays.sort(array);
// Convert the sorted character array to a string as map Key
String key = new String(array);
//map.getOrDefault(key,defaultValue): When Map There's this in the collection key when , Just use this key Corresponding value value , If you don't have this key Just use the default values defaultValue
List<String> list = map.getOrDefault(key,new ArrayList<String>());
// Save the current string to list Collection
list.add(str);
// Deposit it in map in
map.put(key,list);
}
return new ArrayList<List<String>>(map.values());
}
}
Code instructions
Note see ...
边栏推荐
- Natural language processing series (I) introduction overview
- DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告
- Matlab paper chart standard format output (dry goods)
- MATLAB论文图表标准格式输出(干货)
- [深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
- “百度杯”CTF比赛 九月场,Web:Upload
- 【Hot100】33. 搜索旋转排序数组
- go 指针
- mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
- Rocky basic command 3
猜你喜欢
Pycharm installation third party library diagram
Changing JS code has no effect
南理工在线交流群
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
Word document injection (tracking word documents) incomplete
Principle and configuration of RSTP protocol
Win10——轻量级小工具
CloudCompare——点云切片
华为推送服务内容,阅读笔记
FPGA 学习笔记:Vivado 2019.1 添加 IP MicroBlaze
随机推荐
Reverse Polish notation
Rocky basic command 3
Put functions in modules
聊聊异步编程的 7 种实现方式
Flutter InkWell & Ink组件
山东大学暑期实训一20220620
Summary and arrangement of JPA specifications
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
程序员成长第八篇:做好测试工作
Flutter 绘制波浪移动动画效果,曲线和折线图
什么是网络端口
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
Detailed explanation of navigation component of openharmony application development
Parsing XML using Dom4j
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
JS to determine whether an element exists in the array (four methods)
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
go 指针
Rocky basics 1