算法:字符串数组中有多少种字符串

题目描述

只由小写字母(a~z)组成的一批字符串,都放在字符类型的数组String[] arr中,

如果其中某两个字符串,所含有的字符种类完全一样,就将两个字符串算作一类 比如:baacba和bac就算作一类

虽然长度不一样,但是所含字符的种类完全一样(a、b、c) 返回arr中有多少类?

题目解析

位运算

思路

一共有29种字符,一个整数有32位。

用这个整数的第0位表示a是否出现过,1是否出现过…

最后看这个set中还留下了多少个不同的整数

实现

class Solution {
public:
    int types(std::vector<std::string> &arr) {
        std::set<int> types;
        for(auto str : arr){
            int N = str.size();
            int key = 0;
            for (int i = 0; i < N ; ++i) {
                key |= (1 << (str[i] - 'a'));
            }
            types.emplace(key);
        }
        
        return types.size();
    }
};

对数器

int types(std::vector<std::string> &arr) {
    std::set<std::string> types;
    for(auto str : arr){
        int N = str.size();
        std::vector<bool> map(26);
        for (int i = 0; i < N ; ++i) {
            map[str[i] - 'a'] = true;
        }

        std::string key;
        for (int i = 0; i < 26; ++i) {
            if(map[i]){
                key += std::to_string((char)(i + 'a'));
            }
        }
        types.emplace(key);
    }

    return types.size();
}

std::default_random_engine e;
std::string random_string(std::string::size_type length)
{
    static auto& chrs =
                        "abcdefghijklmnopqrstuvwxyz";

    thread_local static std::mt19937 rg{std::random_device{}()};
    thread_local static std::uniform_int_distribution<std::string::size_type> pick(0, sizeof(chrs) - 2);

    std::string s;
    s.reserve(length);
    while(length--)
        s += chrs[pick(rg)];

    return s;
}
std::vector<std::string> generateRandom(int maxVecLen, int maxStrLen){
    std::uniform_int_distribution<int> distS(0, maxVecLen);
    std::uniform_int_distribution<int> distV(0, maxStrLen);
    int size = distS(e);
    std::vector<std::string> ans(size);
    for (int i = 0; i < size; ++i) {
        ans[i] = random_string(distV(e));
    }

    return ans;
}
int main() {
    e.seed(time(NULL));
    int strMaxSize = 10;
    int arrMaxSize = 100;
    int testTimes = 500000;
    Solution a;
    printf("test begin, test time : %d\n" , testTimes);
    for (int i = 0; i < 100; ++i) {
        auto vec = generateRandom(arrMaxSize, strMaxSize);
        int ans1 = a.types(vec);
        int ans2 = types(vec);
        if(ans1 != ans2){
            printf("failed\n");
            break;
        }
    }
    printf("test finished, test time : %d\n" , testTimes);
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值