当前位置:网站首页>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 ...
边栏推荐
- RHCSA10
- Flutter draws animation effects of wave movement, curves and line graphs
- 个人组件 - 消息提示
- leetcode:221. Maximum square [essence of DP state transition]
- Solve Unicode decodeerror: 'GBK' codec can't decode byte 0xa2 in position 107
- 53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
- Shuttle INKWELL & ink components
- 手把手带你入门Apache伪静态的配置
- 初次使用腾讯云,解决只能使用webshell连接,不能使用ssh连接。
- What is a network port
猜你喜欢

MMSeg——Mutli-view时序数据检查与可视化

Word document injection (tracking word documents) incomplete

MSTP and eth trunk

OpenHarmony应用开发之Navigation组件详解

国际自动机工程师学会(SAE International)战略投资几何伙伴

SAE international strategic investment geometry partner

解决uni-app配置页面、tabBar无效问题

Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法

uni-app开发语音识别app,讲究的就是简单快速。

【服务器数据恢复】某品牌服务器存储raid5数据恢复案例
随机推荐
事务的基本特性和隔离级别
STM32 and motor development (from architecture diagram to documentation)
私有地址有那些
爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
“百度杯”CTF比赛 九月场,Web:Upload
JS to determine whether an element exists in the array (four methods)
Integer ==比较会自动拆箱 该变量不能赋值为空
Flutter 3.0更新后如何应用到小程序开发中
潘多拉 IOT 开发板学习(HAL 库)—— 实验7 窗口看门狗实验(学习笔记)
APICloud Studio3 API管理与调试使用教程
Go pointer
MySQL - database query - sort query, paging query
C# 对象存储
聊聊异步编程的 7 种实现方式
OpenHarmony应用开发之Navigation组件详解
What is a network port
Actual combat simulation │ JWT login authentication
Apicloud studio3 API management and debugging tutorial
CloudCompare——点云切片
华为推送服务内容,阅读笔记