893. Groups of Special-Equivalent Strings*

893. Groups of Special-Equivalent Strings*

https://leetcode.com/problems/groups-of-special-equivalent-strings/

题目描述

You are given an array A of strings.

A move onto S consists of swapping any two even indexed characters of S, or any two odd indexed characters of S.

Two strings S and T are special-equivalent if after any number of moves onto S, S == T.

For example, S = "zzxy" and T = "xyzz" are special-equivalent because we may make the moves "zzxy" -> "xzzy" -> "xyzz" that swap S[0] and S[2], then S[1] and S[3].

Now, a group of special-equivalent strings from A is a non-empty subset of A such that:

  1. Every pair of strings in the group are special equivalent, and;
  2. The group is the largest size possible (ie., there isn’t a string S not in the group such that S is special equivalent to every string in the group)
    Return the number of groups of special-equivalent strings from A.

Example 1:

Input: ["abcd","cdab","cbad","xyzz","zzxy","zzyx"]
Output: 3
Explanation: 
One group is ["abcd", "cdab", "cbad"], since they are all pairwise special equivalent, and none of the other strings are all pairwise special equivalent to these.

The other two groups are ["xyzz", "zzxy"] and ["zzyx"].  Note that in particular, "zzxy" is not special equivalent to "zzyx".

Example 2:

Input: ["abc","acb","bac","bca","cab","cba"]
Output: 3

Note:

  • 1 <= A.length <= 1000
  • 1 <= A[i].length <= 20
  • All A[i] have the same length.
  • All A[i] consist of only lowercase letters.

C++ 实现 1

参考: C++ Simple Solution, 作者的想法是:

General Idea:

  1. Split strings in two to substrings, 1 with even indexed characters, and 1 with odd
  2. Sort the two substrings (We do this because if you can swap on string with another, when sorted they will equal each other because they must have the same characters)
  3. Insert your pair of strings into set, this will keep track of the unique “groups”
  4. Rerturn the size of your set

这道题关键在于如何判定两个字符串是 special-equivalent 的, 方法是: 对于 A 中每个字符串 w, 按照索引的奇偶, 将其分为两个子字符串, 并分别对两个子字符串进行排序. 按照上面的操作, 如果两个字符串是 special-equivalent 的, 那么排序之后的结果是相同的.

另外代码中使用了 set 而不是 unordered_set, 后者需要提供 Hash 函数, 否则无法声明 unordered_set<pair<string, string>>.

class Solution {
public:
    int numSpecialEquivGroups(vector<string>& A) {
        set<pair<string, string>> record;
        for (auto &w : A) {
            pair<string, string> p;
            for (int i = 0; i < w.size(); ++ i) {
                if (i % 2 == 0) p.first += w[i];
                else p.second += w[i];
            }
            std::sort(p.first.begin(), p.first.end());
            std::sort(p.second.begin(), p.second.end());
            record.insert(p);
        }
        return record.size();
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值