c语言一维数组怎么快速排列
244
2022-11-14
[leetcode] 893. Groups of Special-Equivalent Strings
Description
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:
Every pair of strings in the group are special equivalent, and;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: 3Explanation: 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 <= 10001 <= A[i].length <= 20All A[i] have the same length.All A[i] consist of only lowercase letters.
分析
题目的意思是:分类特别相等的字符串,这道题我看了好多遍没看明白,后面发现是奇数位置和偶数位置排序后构成的数相等才算相等,所以这里用一个set集合来统计一下个数就行了。如果看懂题目才好做,否则估计跟我一样郁闷了,哈哈哈。
代码
class Solution: def numSpecialEquivGroups(self, A: List[str]) -> int: signature = set() for s in A: signature.add(''.join(sorted(s[::2]))+''.join(sorted(s[1::2]))) return len(signature)
参考文献
[LeetCode] Python O(n * k lg k) sol. by signature. 90%+ [w/ Hint]
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~