文章目录
session Ⅰ Algorithm
problem Ⅰ
49. Group Anagrams
Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly 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”]]
my solution Ⅰ Time Limit Exceeded
class Solution {
public:
bool match(vector<int>& word1, vector<int>& word2){
for(int i=0; i<26; i++)
if(word1[i] != word2[i])return false;
return true;
}
vector<vector<string>> groupAnagrams(vector<string>& strs) {
int len = strs.size();
vector<int> record(len, 0);
vector<vector<string>> res;
for(int i=0; i<len; i++){
if(record[i]==0){
vector<string> tmp;
tmp.push_back(strs[i]);
record[i]=1;
if(i==len-1){
res.push_back(tmp);