[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 number of moves, S == T.
A move consists of choosing two indices i
and j
with 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/
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 893. Groups of Special-Equivalent Strings 特殊字符串的群组的更多相关文章
- Equivalent Strings (字符串相等?)
Equivalent Strings E - 暴力求解.DFS Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I ...
- 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 ...
- LeetCode 893. Groups of Special-Equivalent Strings (特殊等价字符串组)
题目标签:String 题目可以让在 偶数位置的 chars 互换, 也可以让 在 奇数位置的 chars 互换. 所以为了 return 正确的 group 数量,需要把 那些重复的 给排除掉. 可 ...
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 893. Groups of Special-Equivalent Strings - LeetCode
Question 893. Groups of Special-Equivalent Strings Solution 题目大意: AB两个字符串相等的条件是:A中偶数位出现的字符与B中偶数位出现的字 ...
- 【Leetcode_easy】893. Groups of Special-Equivalent Strings
problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...
- Equivalent Strings
Equivalent Strings 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/E 题意: 给出 ...
- Codeforces Round #313 (Div. 1) B. Equivalent Strings
Equivalent Strings Problem's Link: http://codeforces.com/contest/559/problem/B Mean: 给定两个等长串s1,s2,判断 ...
- Codeforces Round #313 (Div. 2) D. Equivalent Strings
D. Equivalent Strings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/ ...
随机推荐
- COMP222 - 2019
COMP222 - 2019 - Second CA AssignmentIndividual courseworkTrain Deep Learning AgentsAssessment Infor ...
- F#周报2019年第19期
新闻 介绍.NET 5 发布.NET Core 3.0预览版5以及F#的REPL OpenFsharp CFP开启 F#的Giraffe服务端stub生成器被添加到openapi-generator中 ...
- yii2.0的学习之旅(一)
一. 通过composer安装yii2.0项目 *本文是根据您已经安装了composer (1)跳转到项目根目录 cd /xxxx/www (2)下载插件 composer global requir ...
- 【JS】---4用JS获取地址栏参数方法
用JS获取地址栏参数方法 // 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new Reg ...
- checked,unchecked
static void Main(string[] args) { byte b1 = 100; byte b2 = 250; //Checked try { byte sum = checked ( ...
- Java生鲜电商平台-系统报表设计与架构
Java生鲜电商平台-系统报表设计与架构 说明:任何一个运行的平台都需要一个很清楚的报表来显示,那么作为Java开源生鲜电商平台而言,我们应该如何设计报表呢?或者说我们希望报表来看到什么数据呢? ...
- springcloud分布式事务Atomikos实例
0.JTA(Java Transaction Manager)的介绍 (1)jta与jdbc 简单的说 jta是多库的事务 jdbc是单库的事务 (2)XA与JTA XA : XA是一个规范或是一个事 ...
- webpack4 css modules
demo 代码点此,webpack4 中通过 css-loader 开启 css 模块化, 开始前先做点准备工作. 不了解 css 模块化的,可以前往查看github_css_modules. ##准 ...
- [b0017] python 归纳 (三)_类名当参数传入
# -*- coding: UTF-8 -*- """ 测试传入类名 总结: 似乎python里面的一切东西都可以当参数传入 函数或者方法 ""&qu ...
- 2.GoF 的 23 种设计模式的分类和功能
1. 根据目的来分 根据模式是用来完成什么工作来划分,这种方式可分为创建型模式.结构型模式和行为型模式 3 种. 创建型模式:用于描述“怎样创建对象”,它的主要特点是“将对象的创建与使用分离”.GoF ...