链接:https://leetcode-cn.com/problems/groups-of-special-equivalent-strings/
统计奇偶下标各字母的出现次数,或者分别对奇偶下标的子串进行排序。
python代码:
class Solution(object):
def numSpecialEquivGroups(self, A):
"""
:type A: List[str]
:rtype: int
"""
s = set()
for string in A:
count = [0]*52
for i, letter in enumerate(string):
count[ord(letter) - ord('a') + 26 * (i%2)] += 1
s.add(tuple(count))
return len(s)
class Solution(object):
def numSpecialEquivGroups(self, A):
"""
:type A: List[str]
:rtype: int
"""
s = set()
for string in A:
changedstr = ''.join(sorted(string[0::2])+sorted(string[1::2]))
s.add(changedstr)
return len(s)