【leetcode】893. Groups of Special-Equivalent Strings
Algorithm
【leetcode】893. Groups of Special-Equivalent Strings
https://leetcode.com/problems/groups-of-special-equivalent-strings/
1)problem
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 <= 10001 <= A[i].length <= 20- All
A[i]have the same length. - All
A[i]consist of only lowercase letters.
2)answer
统计奇数位和偶数位的个字符出现的次数,如果 S0 串和 S1 串统计的结果相同,则它们是 special-equivalent 的。
3)solution
- 将字符串拆分为两个子字符串,1个包含偶数索引字符,1个包含奇数字符串
- 对两个子字符串进行排序(这样做是因为如果可以将字符串与另一个字符串交换,那么在排序时它们将彼此相等,因为它们必须具有相同的字符)
- 将一对字符串插入集合中,这将跟踪唯一的“组”
- 重新调整集合的大小
符合上面两个条件的是一组。
参考:https://blog.csdn.net/g_r_c/article/details/82079678
unordered_set使用方法
.find() 返回一个迭代器。这个迭代器指向和参数哈希值匹配的元素,如果没有匹配的元素,会返回这个容器的结束迭代器。
.end() 返回指向容器末尾位置的迭代器
.insert() 插入元素
视频:
https://www.youtube.com/watch?v=WJ4NtyrakT0&feature=youtu.be
图片示例:

C++实现代码:
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <algorithm>
using std::vector;
using std::string;
using std::unordered_set;
class Solution {
public:
int numSpecialEquivGroups(vector<string>& A) {
unordered_set<string> st;
for (int i = 0; i < A.size(); ++i) {
string odd = "";
string even = "";
for (int j = 0; j < A[i].size(); ++j) {
// 奇偶位的值
if (j % 2 == 0)
odd += A[i][j];
else
even += A[i][j];
}
// 排序奇偶位的值
sort(odd.begin(), odd.end());
sort(even.begin(), even.end());
st.insert(odd + even);
}
return st.size();
}
};
int main()
{
Solution solution;
vector<string> A1 = { "abcd", "cdab", "adcb", "cbad" };
vector<string> A2 = { "abc", "acb", "bac", "bca", "cab", "cba" };
int res1 = solution.numSpecialEquivGroups(A1);
int res2 = solution.numSpecialEquivGroups(A2);
}
Python实现:
def numSpecialEquivGroups(self,A):
s = set()
for w in A:
even = ''.join(sorted(w[0::2]))
odd = ''.join(sorted(w[1::2]))
s.add(odd+even)
return len(s)
【leetcode】893. Groups of Special-Equivalent Strings的更多相关文章
- 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode_easy】893. Groups of Special-Equivalent Strings
problem 893. Groups of Special-Equivalent Strings 题意: 感觉参考代码也是有点问题的... 参考 1. Leetcode_easy_893. Grou ...
- 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- 【LeetCode】5685. 交替合并字符串 Merge Strings Alternately (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,交替合并字符串,Merge Strings Alternately,刷题群 目 ...
- 【LeetCode】583. Delete Operation for Two Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】583. Delete Operation for Two Strings
583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of st ...
- 【leetcode】1247. Minimum Swaps to Make Strings Equal
题目如下: You are given two strings s1 and s2 of equal length consisting of letters "x" and &q ...
- 【leetcode】1071. Greatest Common Divisor of Strings
题目如下: For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concate ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
随机推荐
- CodeForces755F 贪心 + 多重背包二进制优化
https://cn.vjudge.net/problem/615831/origin 题意 n个人; 计划是每个人都拿一个礼物来送给一个除了自己之外的人; 如果一个人没有送出礼物,那么它和它送礼 ...
- Spring入门初体验
Spring其实就是一个容器,让我们更方便的拿到想要的对象. 1.编写一个service // userService.java public interface userService { publ ...
- Hadoop生态圈-单点登录框架之CAS(Central Authentication Service)部署
Hadoop生态圈-单点登录框架之CAS(Central Authentication Service)部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.CAS简介 CAS( ...
- mysql 快速生成删除数据库中所有的表的语句
SELECT concat('DROP TABLE IF EXISTS ', table_name, ';') FROM information_schema.tables WHERE table_s ...
- Java测试Junit
Junit就是做测试用的,想想平常我们是怎么测试我们的方法或者类的,是不是在main方法里面去调用?这样有缺点: 1.每次都要在main方法里面写测试,假如我要上线新系统,里面有1000个方法需要测试 ...
- Git(时光机-版本回退)
现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...
- SSRF漏洞分析与利用
转自:http://www.4o4notfound.org/index.php/archives/33/ 前言:总结了一些常见的姿势,以PHP为例,先上一张脑图,划√的是本文接下来实际操作的 0x01 ...
- Subversion Edge部署
1.配置java的jdk vim /etc/profile 上面文件中加入export JAVA_HOME=/****/jdk*****,然后在path中加入$JAVA_HOME/bin: ,保存退 ...
- 转载 --- SKLearn中预测准确率函数介绍
混淆矩阵 confusion_matrix 下面将一一给出'tp','fp','fn'的具体含义: 准确率: 所有识别为"1"的数据中,正确的比率是多少. 如识别出来100个结果是 ...
- iOS下如何阻止橡皮筋效果
$(document).ready(function(){ var stopScrolling = function(event) { event.preventDefault(); } docume ...