day 22: 49-valid-anagrams

This commit is contained in:
Kaushik Narayan R 2023-10-21 13:28:40 -07:00
parent df57a62275
commit 8324410cbe
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,31 @@
#include "soln.hpp"
int main()
{
vector<string> strs{"eat", "tea", "tan", "ate", "nat", "bat"};
vector<vector<string>> answer;
answer.push_back(vector<string>{"bat"});
answer.push_back(vector<string>{"nat", "tan"});
answer.push_back(vector<string>{"ate", "eat", "tea"});
Solution soln;
vector<vector<string>> result = soln.groupAnagrams(strs);
cout << "Result set: " << endl;
for (vector<string> strset : result)
{
for (string str : strset)
{
cout << str << " ";
}
cout << endl;
}
cout << "Answer set: " << endl;
for (vector<string> strset : answer)
{
for (string str : strset)
{
cout << str << " ";
}
cout << endl;
}
return 0;
}

View File

@ -0,0 +1,18 @@
#include "soln.hpp"
vector<vector<string>> Solution::groupAnagrams(vector<string> &strs)
{
unordered_map<string, vector<string>> str_map;
for (string s : strs)
{
string t = s;
sort(t.begin(), t.end());
str_map[t].push_back(s);
}
vector<vector<string>> anag;
for (auto it : str_map)
{
anag.push_back(it.second);
}
return anag;
}

View File

@ -0,0 +1,7 @@
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<string>> groupAnagrams(vector<string> &strs);
};