晨太狼之狼堡

二货一枚快乐多

Anagrams

Given an array of strings, return all groups of strings that are anagrams.

Note: All inputs will be in lower-case.


Solution:

class Solution {
public:
    vector<string> anagrams(vector<string>& strs) {
        vector<string> res;
        unordered_map<string, int> um;
        for(int i = 0; i < strs.size(); ++i)
        {
            string str = strs[i];
            sort(str.begin(), str.end());
            if(um.count(str) == 0) um[str] = i;
            else
            {
                if(um[str] >= 0)
                {
                    res.push_back(strs[um[str]]);
                    um[str] = -1;
                }
                res.push_back(strs[i]);
            }
        }
        
        return res;
    }
};


阅读更多
文章标签: Hash Table String
个人分类: LeetCode
上一篇Implement strStr()
下一篇Text Justification
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭