当前位置:网站首页>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 ...
边栏推荐
- [notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
- Backup and restore of Android local SQLite database
- The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
- Lb10s-asemi rectifier bridge lb10s
- Hiengine: comparable to the local cloud native memory database engine
- Go pointer
- Difference between avc1 and H264
- Cloudcompare - point cloud slice
- STM32 and motor development (from architecture diagram to documentation)
- 记录一下在深度学习-一些bug处理
猜你喜欢
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
ASEMI整流桥HD06参数,HD06图片,HD06应用
OpenHarmony应用开发之Navigation组件详解
"Baidu Cup" CTF competition in September, web:sql
Word document injection (tracking word documents) incomplete
STM32 and motor development (from architecture diagram to documentation)
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
Shandong University Summer Training - 20220620
“百度杯”CTF比赛 九月场,Web:Upload
Pycharm installation third party library diagram
随机推荐
Rocky basic command 3
Can and can FD
MATLAB论文图表标准格式输出(干货)
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
APICloud Studio3 API管理与调试使用教程
使用Dom4j解析XML
[daily question] 1200 Minimum absolute difference
Asemi rectifier bridge hd06 parameters, hd06 pictures, hd06 applications
华为推送服务内容,阅读笔记
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Write macro with word
Apicloud studio3 API management and debugging tutorial
Cf:a. the third three number problem
RHCSA10
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
CloudCompare——点云切片
Parsing XML using Dom4j
C object storage
Difference between avc1 and H264