乱序字符串

来源:互联网 发布:上瘾网络剧第二季泰国 编辑:程序博客网 时间:2024/04/29 18:50

给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。

样例

对于字符串数组 ["lint","intl","inlt","code"]

返回 ["lint","inlt","intl"]

class Solution {public:        /**     * @param strs: A list of strings     * @return: A list of strings     */    vector<string> anagrams(vector<string> &strs) {        // write your code here        int n=strs.size();        vector<string>res;        unordered_map<string,int>m;        if(n==0) return res;        string first;        int t=0;        for(int i=0;i<n;i++){            string tmp=strs[i];            sort(tmp.begin(),tmp.end());            m[tmp]++;        }        for(int i=0;i<n;i++){            string tmp=strs[i];            sort(tmp.begin(),tmp.end());            if(m[tmp]>1) res.push_back(strs[i]);        }                return res;    }};


0 0
原创粉丝点击