当前位置: 代码迷 >> 综合 >> leetcode 893. Groups of Special-Equivalent Strings (easy)
  详细解决方案

leetcode 893. Groups of Special-Equivalent Strings (easy)

热度:43   发布时间:2024-01-05 00:21:52.0

题目链接

题目大意:

对某个字符串:交换奇位数,或者交换偶位数上的值以后,与另一个字符串相同,则这两个字符串归类为一组。
现给一堆字符串,按以上规则判断有几组。


思路:

类似统计的首先想到用set这种唯一容器去存储不同组。
如何判断是否为一组呢? 简单来看就是该组中的字符串,所有奇(偶)数上的所有字符种类、个数相同。统计种类和个数方法也很多,可以自行替换,这里直接用拼接字符串再排序存储的方法。

class Solution
{
    
public:int numSpecialEquivGroups(vector<string> &A){
    set<string> res;for (auto str : A){
    pair<string, string> temp;for (int i = 0; i < str.size(); i++){
    if (i % 2 == 0)temp.first += str[i];elsetemp.second += str[i];}sort(temp.first.begin(), temp.first.end());sort(temp.second.begin(), temp.second.end());res.insert(temp.first + temp.second);}return res.size();}
};
  相关解决方案