当前位置:网站首页>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 separatelyThe 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 ...
边栏推荐
- Parsing XML using Dom4j
- “百度杯”CTF比赛 九月场,Web:Upload
- 碎片化知识管理工具Memos
- Actual combat simulation │ JWT login authentication
- How to realize batch sending when fishing
- Detailed explanation of navigation component of openharmony application development
- 蜀天梦图×微言科技丨达梦图数据库朋友圈+1
- Principle and configuration of RSTP protocol
- 前缀、中缀、后缀表达式「建议收藏」
- go 字符串操作
猜你喜欢

Hiengine: comparable to the local cloud native memory database engine

LB10S-ASEMI整流桥LB10S

精彩速递|腾讯云数据库6月刊

Get to know linkerd project for the first time

ASEMI整流桥HD06参数,HD06图片,HD06应用

Win10——轻量级小工具

UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
![[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel](/img/b6/f9da8a36167db10c9a92dabb166c81.png)
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel

CAN和CAN FD

Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
随机推荐
The solution of outputting 64 bits from printf format%lld of cross platform (32bit and 64bit)
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
记录一下在深度学习-一些bug处理
How to choose note taking software? Comparison and evaluation of notion, flowus and WOLAI
Shuttle INKWELL & ink components
It's too convenient. You can complete the code release and approval by nailing it!
Detailed explanation of navigation component of openharmony application development
Win10——轻量级小工具
“百度杯”CTF比赛 九月场,Web:SQL
Natural language processing series (I) introduction overview
初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
Halcon template matching actual code (I)
Flutter 3.0更新后如何应用到小程序开发中
聊聊异步编程的 7 种实现方式
Rocky基础命令3
Principle and performance analysis of lepton lossless compression
【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
【Hot100】33. Search rotation sort array
Difference between avc1 and H264
C object storage