【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/groups-of-special-equivalent-strings/description/
题目描述
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.
题目大意
可以对一个字符串的所有奇数位置或者偶数位置进行任意的调换顺序。如果两个字符串在经历了上面的操作之后,可以做到完全相等,那么就属于题目中的一个组。现在就要我们求最终分为几个组。
解题方法
理解了题意之后我们可以直接使用暴力的方法去求解了,毕竟是Easy的题目,没那么难。
把这个数组中所有的奇数位置和偶数位置的字符分别取出来,进行排序再合并。把合并之后的结果放入到一个set里,然后统计set中字符串的个数也就是题目中要求的组数。
代码如下:
class Solution(object):
def numSpecialEquivGroups(self, A):
"""
:type A: List[str]
:rtype: int
"""
B = set()
for a in A:
B.add(''.join(sorted(a[0::2])) + ''.join(sorted(a[1::2])))
return len(B)
参考资料:无
日期
2018 年 8 月 26 日 —— 珍爱生命,远离DD!
2018 年 11 月 6 日 —— 腰酸背痛要废了
【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)的更多相关文章
- 【LeetCode】94. Binary Tree Inorder Traversal 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 递归 迭代 日期 题目地址:https://leetcode.c ...
- 【LeetCode】341. Flatten Nested List Iterator 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归+队列 栈 日期 题目地址:https://lee ...
- 【LeetCode】589. N-ary Tree Preorder Traversal 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
随机推荐
- Linux— 查看系统发布版本信息
[root@zf-test-web01-4 ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core)
- java中的Arrays类
今天刚接触了数组,学到了几个比较常用的方法 Fill方法:给数组赋值 sort方法:给数组升序 equals方法:比较数组中元素 值是否相等 binarySearch方法:对排序好的数组进行二分查找法 ...
- 详解 Rainbond Ingress 泛解析域名机制
Rainbond 作为一款云原生应用管理平台,天生带有引导南北向网络流量的分布式网关 rbd-gateway.区别于一般的 Ingress 配置中,用户需要自行定义域名的使用体验,Rainbond 的 ...
- android studio Please configure Android SDK / please select Android SDK
有可能是sdk文件损坏造成的. file->settings->appearance&behavior->system settings->android sdk-&g ...
- 在windows 10家庭版上安装docker的步骤
本人之前写Redis书和Spring Cloud Alibaba书时,发现一些分布式组件更适合安装在linux环境,而在搭建Redis等集群时,更需要linux环境. 本人日常练习代码和写书所用的机器 ...
- act.四级
act的词源是do, 干着或干了的事情也可以叫act.action: doing sth; act: n. action, v. do; activity: busy, energetic, or占据 ...
- 大数据学习day38----数据仓库01-----区域字典的生成
更多内容见文档 1. 区域字典的生成 mysql中有如下表格数据 现要将这类数据转换成(GEOHASH码, 省,市,区)如下所示 (1)第一步:在mysql中使用sql语句对表格数据进行整理(此处使用 ...
- nodeJs-process对象
JavaScript 标准参考教程(alpha) 草稿二:Node.js process对象 GitHub TOP process对象 来自<JavaScript 标准参考教程(alpha)&g ...
- When does compiler create default and copy constructors in C++?
In C++, compiler creates a default constructor if we don't define our own constructor (See this). Co ...
- class.getName()和class.getSimpleName()的区别
根据API中的定义: Class.getName():以String的形式,返回Class对象的"实体"名称: Class.getSimpleName():获取源代码中给出的&qu ...