Lintcode(4)-乱序字符串

Q:
给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。
对于字符串数组 [“lint”,”intl”,”inlt”,”code”]
返回 [“lint”,”inlt”,”intl”]
A:

class Solution {
public:
    /**
     * @param strs: A list of strings
     * @return: A list of strings
     */
    vector<string> anagrams(vector<string> &strs) {
        if (strs.empty()) {
            return strs;
        }
        typedef unsigned int uint32;
        typedef std::set<int> index_set;
        typedef std::map<std::string, index_set > index_map;
        uint32 si = strs.size();
        index_map stats;
        std::vector<std::string> result;
        for ( uint32 i = 0; i < si; ++i ) {
            std::string temp(strs[i].begin(), strs[i].end());
            std::sort(temp.begin(), temp.end());
            stats[temp].insert(i);
        }
        for ( index_map::const_iterator it = stats.begin(); it != stats.end(); ++it ) {
            const index_set& t = it->second;
            if (t.size() > 1) {
                for ( index_set::const_iterator it2 = t.begin(); it2 != t.end(); ++it2 ) {
                    result.push_back(strs[*it2]);
                }
            }
        }
        return result;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值