# 【leetcode】 Anagrams

86人阅读 评论(0)


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

class Solution
{
public:
string fun(vector<string> str)
{
string a="";

map<string,vector<string>>mp;
cout<<str.size()<<endl;
for(int i=0;i<str.size();i++)
{
string temp=str[i];
sort(temp.begin(),temp.end());
mp[temp].push_back(str[i]);
}
for(map<string,vector<string>>::iterator iter=mp.begin();iter!=mp.end();iter++)
{
if(iter->second.size()>1)
{
for(vector<string>::iterator it=iter->second.begin();it!=iter->second.end();it++)
{
a+=*it;
a.append(1,',');

}
}
}
return a;
}
};

class Solution2
{
public:
string fun(vector<string> str)
{
string a="";

map<string,int>mp;
vector<string>ans;
cout<<str.size()<<endl;
for(int i=0;i<str.size();i++)
{
string temp=str[i];
sort(temp.begin(),temp.end());
map<string,int>::iterator iter=mp.find(temp);
if(iter==mp.end())
{
mp[temp]=1;
}
else
mp[temp]++;
}
for(int i=0;i<str.size();i++)
{
string temp=str[i];
sort(temp.begin(),temp.end());
if(mp.find(temp)!=mp.end()&&mp[temp]>1)
{
a+=str[i];
a.append(1,',');
}
}
return a;
}
};

void main()
{
Solution solution2;
string str[5]={" eat","a te","li mk","mil k","tae "};
vector<string>s(str,str+5);
string result;
/*for(int i=0;i<5;i++)
s.push_back(str[i]);*/
result=solution2.fun(s);
//for(vector<string>::iterator iter=result.begin();iter!=result.end();iter++)
//cout<<*iter<<endl;
cout<<result;

}



#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

class Solution2
{
public:
string fun(vector<string> str)
{
string a="";

map<string,int>mp;
vector<string>ans;
cout<<str.size()<<endl;
for(int i=0;i<str.size();i++)
{
string temp=str[i];
sort(temp.begin(),temp.end());
map<string,int>::iterator iter=mp.find(temp);
if(iter==mp.end())
{
mp[temp]=1;
}
else
mp[temp]++;
}
for(int i=0;i<str.size();i++)
{
string temp=str[i];
sort(temp.begin(),temp.end());
if(mp.find(temp)!=mp.end()&&mp[temp]>1)
{
a+=str[i];
a.append(1,',');
}
}
return a;
}
};

void main()
{
Solution solution2;
string str[5]={" eat","a te","li mk","mil k","tae "};
vector<string>s(str,str+5);
string result;
/*for(int i=0;i<5;i++)
s.push_back(str[i]);*/
result=solution2.fun(s);
//for(vector<string>::iterator iter=result.begin();iter!=result.end();iter++)
//cout<<*iter<<endl;
cout<<result;

}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：7194次
• 积分：567
• 等级：
• 排名：千里之外
• 原创：51篇
• 转载：6篇
• 译文：0篇
• 评论：1条
最新评论