Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- bool isPermute(string str1,string str2)
- {
- if(str1.length()==0&&str2.length()==0)
- return true;
- if(str1.length()!=str2.length())
- return false;
- vector<int>s1(26,0);
- vector<int>s2(26,0);
- for(int i=0;i<str1.length();i++)
- {
- s1[((int)str1[i])%26]++;
- s2[((int)str2[i])%26]++;
- }
- for(int i=0;i<s1.size();i++)
- {
- if (s1[i]!=s2[i])
- return false;
- }
- return true;
- }
- vector<vector<string> > groupAnagrams(vector<string>& strs) {
- vector<vector<string> > res;
- vector<string> f;
- int i=0;
- while(!strs.empty())
- {
- vector<string>temp;
- temp.push_back(strs[i]);
- int j=i+1;
- while(j<strs.size())
- {
- while(isPermute(strs[i],strs[j]))
- {
- temp.push_back(strs[j]);
- strs.erase(strs.begin()+j);
- if (j+1>strs.size())
- break;
- if (isPermute(strs[i],strs[j]))
- continue;
- else
- j+=1;
- }
- j+=1;
- }
- if (!temp.empty())
- {
- res.push_back(temp);
- temp.clear();
- }
- strs.erase(strs.begin()+i);
- }
- return res;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement