当前位置:网站首页>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 ...
边栏推荐
- "Baidu Cup" CTF competition in September, web:sql
- 关于 Notion-Like 工具的反思和畅想
- 私有地址有那些
- Flutter draws animation effects of wave movement, curves and line graphs
- The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
- 国际自动机工程师学会(SAE International)战略投资几何伙伴
- JS to determine whether an element exists in the array (four methods)
- 先写API文档还是先写代码?
- Navigation property and entityset usage in SAP segw transaction code
- “百度杯”CTF比赛 九月场,Web:Upload
猜你喜欢

Reverse Polish notation

数据湖(七):Iceberg概念及回顾什么是数据湖
![[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2](/img/52/5e85743b1817de96a52e02b92fd08c.png)
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2

Flutter 绘制波浪移动动画效果,曲线和折线图

The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched

Word document injection (tracking word documents) incomplete

Cloudcompare - point cloud slice

DataPipeline双料入选中国信通院2022数智化图谱、数据库发展报告

Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?

Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
随机推荐
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
山东大学暑期实训一20220620
Put functions in modules
MMSeg——Mutli-view时序数据检查与可视化
Reflection and imagination on the notation like tool
mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
STM32 and motor development (from architecture diagram to documentation)
Go string operation
手把手带你入门Apache伪静态的配置
Parsing XML using Dom4j
MySQL splits strings for conditional queries
go map
精彩速递|腾讯云数据库6月刊
【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
go 指针
leetcode 10. Regular expression matching regular expression matching (difficult)
A detailed explanation of ASCII code, Unicode and UTF-8
Run, open circuit
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
There is no monitoring and no operation and maintenance. The following is the commonly used script monitoring in monitoring