当前位置:网站首页>Leetcode sword finger offer II 033 Modified phrase
Leetcode sword finger offer II 033 Modified phrase
2022-06-12 05:56:00 【Taoist scholar】
link
The finger of the sword Offer II 033. Morpheme phrase
subject
Given an array of strings
strs, take A modifier Put together . You can return a list of results in any order .Be careful : If each character in two strings appears the same number of times , They are called mutuals .
Example
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"]]
Tips
1 <= strs.length <= 10e40 <= strs[i].length <= 100strs[i]Only lowercase letters
Ideas
Anagrams are two words with the same number of letters , therefore , If you sort the letters of two anagrams , The final two words must be equal .
therefore , We can use the sorted words of each word as the index of the hash table , The original word is stored in the string array corresponding to the index , Finally, all arrays in the hash table can be stored in the result array .
C++ Code
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string,vector<string>> map;
vector<vector<string>> ans;
for(auto s:strs)
{
string tmp=s; //tmp Yes s Sorted string s For the original string
sort(tmp.begin(),tmp.end());
map[tmp].push_back(s); // After sorting tmp The original string of the same string s Save to hash table
}
for(auto &[x,y]: map) ans.push_back(y); // Store the string array stored in the hash table one by one ans
return ans;
}
};边栏推荐
- 基于LFFD模型目标检测自动标注生成xml文件
- 为什么数据库不使用二叉树、红黑树、B树、Hash表? 而是使用了B+树
- Jpg format and XML format files are separated into different folders
- Oracle EBS interface/api (34) - update vendor API
- Recursive implementation of exponential, permutation and combination enumerations
- XML参数架构,同一MTK SW版本兼容两套不同的音频参数
- nus_ data_ Handler source code interprets data types such as structure
- [long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation
- A month's worth of DDD will help you master it
- Makefile文件编写快速掌握
猜你喜欢

Introduction to sringmvc

Introduction to Internet Protocol

为什么联合索引是最左匹配原则?

March 22, 2021

User login 【 I 】

Simple introduction to key Wizard

Data integration framework seatunnel learning notes

CCF noi2022 quota allocation scheme

Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh
![Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]](/img/25/b3c475e2b03c39b7c576b6d01f9d56.jpg)
Leetcode buckle -10 Regular expression matching analysis [recursion and dynamic programming]
随机推荐
Jackson - how to convert the array string with only one map object to list < map >
Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh
Introduction to sringmvc
User login 【 I 】
Unity vscode cannot jump to definition
Halcon 3D 1 Reading 3D data
Recursive implementation of exponential, permutation and combination enumerations
Who is more fierce in network acceleration? New king reappeared in CDN field
Brief summary of software project architecture
C语言-数组的定义方式
Memory model, reference and function supplement of program
[grpc development] go language builds simple server and client
About architecture (in no particular order)
[long time series prediction] the [4] autocorrelation mechanism of aotoformer code explanation
Simple introduction to key Wizard
jpg格式与xml格式文件分离到不同的文件夹
log4j 1. X upgrade to 2 Solution of X dependency incompatibility
POI, easyexcel framework use
Introduction to thread pool: ThreadPoolExecutor
Guns框架多数据源配置,不修改配置文件