You are given an array `A` of strings.

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

move consists of choosing two indices i and jwith i % 2 == j % 2, and swapping S[i] with S[j].

Now, a group of special-equivalent strings from A is a non-empty subset S of A such that any string not in S is not special-equivalent with any string in S.

Return the number of groups of special-equivalent strings from A.

Example 1:

Input: ["a","b","c","a","c","c"]
Output: 3
Explanation: 3 groups ["a","a"], ["b"], ["c","c","c"]

Example 2:

Input: ["aa","bb","ab","ba"]
Output: 4
Explanation: 4 groups ["aa"], ["bb"], ["ab"], ["ba"]

Example 3:

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

Example 4:

Input: ["abcd","cdab","adcb","cbad"]
Output: 1
Explanation: 1 group ["abcd","cdab","adcb","cbad"]

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.

这道题定义了一种特殊相等的关系,就是说对于一个字符串,假如其偶数位字符之间可以互相交换,且其奇数位字符之间可以互相交换,交换后若能跟另一个字符串相等,则这两个字符串是特殊相等的关系。现在给了我们一个字符串数组,将所有特殊相等的字符串放到一个群组中,问最终能有几个不同的群组。最开始的时候博主没仔细审题,以为是随意交换字母,就直接对每个单词进行排序,然后扔到一个 HashSet 中就行了。后来发现只能是奇偶位上互相交换,于是只能现先将奇偶位上的字母分别抽离出来,然后再进行分别排序,之后再合并起来组成一个新的字符串,再丢到 HashSet 中即可,利用 HashSet 的自动去重复功能,这样最终留下来的就是不同的群组了,参见代码如下:

class Solution {
public:
int numSpecialEquivGroups(vector<string>& A) {
unordered_set<string> st;
for (string word : A) {
string even, odd;
for (int i = 0; i < word.size(); ++i) {
if (i % 2 == 0) even += word[i];
else odd += word[i];
}
sort(even.begin(), even.end());
sort(odd.begin(), odd.end());
st.insert(even + odd);
}
return st.size();
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/893

参考资料:

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

https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163413/Java-Concise-Set-Solution

https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163412/C%2B%2B-Simple-Solution

https://leetcode.com/problems/groups-of-special-equivalent-strings/discuss/163891/C%2B%2B-Create-a-signature-for-each-string

[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)

[LeetCode] 893. Groups of Special-Equivalent Strings 特殊字符串的群组的更多相关文章

  1. Equivalent Strings (字符串相等?)

    Equivalent Strings   E - 暴力求解.DFS Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I ...

  2. LeetCode 893 Groups of Special-Equivalent Strings 解题报告

    题目要求 You are given an array A of strings. Two strings S and T are special-equivalent if after any nu ...

  3. LeetCode 893. Groups of Special-Equivalent Strings (特殊等价字符串组)

    题目标签:String 题目可以让在 偶数位置的 chars 互换, 也可以让 在 奇数位置的 chars 互换. 所以为了 return 正确的 group 数量,需要把 那些重复的 给排除掉. 可 ...

  4. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  5. 893. Groups of Special-Equivalent Strings - LeetCode

    Question 893. Groups of Special-Equivalent Strings Solution 题目大意: AB两个字符串相等的条件是:A中偶数位出现的字符与B中偶数位出现的字 ...

  6. 【Leetcode_easy】893. Groups of Special-Equivalent Strings

    problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...

  7. Equivalent Strings

    Equivalent Strings 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/E 题意: 给出 ...

  8. Codeforces Round #313 (Div. 1) B. Equivalent Strings

    Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...

  9. Codeforces Round #313 (Div. 2) D. Equivalent Strings

    D. Equivalent Strings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/ ...

随机推荐

  1. oracle中如何更改一个表的一个字段属性(名称,类型)

    修改字段的属性,名称方法 --修改某一个字段的类型,当该字段不为null时alter table 表名add 字段NUMBER(11,0) default 0 not null;--添加表一个字段 A ...

  2. HTTPS 相关问题

    什么是 HTTPS? HTTPS,是指超文本传输安全协议(Hypertext Transfer Protocol Secure),是一种在 HTTP 协议基础上进行传输加密的安全协议,能够有效保障数据 ...

  3. redis命令之 ----SortedSed(有序集合)

    ZADD ZADD key score member [[score member] [score member] ...] 将一个或多个 member 元素及其 score 值加入到有序集 key  ...

  4. js Iframe与父级页面通信及IE9-兼容性

    一. postMessage window.postMessage()方法安全地启用Window对象之间的跨源通信:例如,在页面和它产生的弹出窗口之间,或者在页面和嵌入其中的iframe之间. 二.语 ...

  5. 搭建 Frp 来远程内网 Windows 和 Linux 机子

    魏刘宏 2019 年 5 月 19 日 一.使用一键脚本搭建服务端 Frp 这个内网穿透项目的官方地址为 https://github.com/fatedier/frp ,不过我们今天搭建服务端时不直 ...

  6. python安装扩展”unable to find vcvarsall.bat“的解决办法

    产生原因: python3.4用的是msvs2010编译的,所以python3.4默认只能认出msvs2010以上版本 python2.7用的是msvs2008编译的,所以python2.7默认只能认 ...

  7. VMware 虚拟机网络配置

    随手记: 近期需要安装几个服务器,编写负载均衡NLB的教程,使用到虚拟机: VMware-workstation-full-15.0.2-10952284.exe cn_windows_server_ ...

  8. React组件安装使用和生命周期函数

    React安装在使用react时 需要安装 两个模块 react react-dom 初始化时 需要用到react-dom中的render方法 具体如下: import ReactDOM from & ...

  9. Context都没弄明白,还怎么做Android开发

    转载:https://www.jianshu.com/p/94e0f9ab3f1d Activity mActivity =new Activity() 作为Android开发者,不知道你有没有思考过 ...

  10. c++的explicit理解

    默认规定 只有一个参数的构造函数也定义了一个隐式转换,将该构造函数对应数据类型的数据转换为该类对象 explicit class A { explicit A(int n); A(char *p); ...