给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
这个题比较好入手,直接选用暴力求解法,一些细节有点类似于摩尔投票,但空间消耗较大。先通过测试再说:
class Solution(object):
def CheckPermutation(self, s1, s2):
"""
:type s1: str
:type s2: str
:rtype: bool
"""
if len(s1) != len(s2):
return False
else:
chose_i = []
chose_j = []
for i in range(len(s1)):
if i not in chose_i:
for j in range(len(s2)):
if j not in chose_j:
if s1[i] == s2[j]:
chose_i.append(i)
chose_j.append(j)
break
if len(chose_i) == len(s1) and len(chose_j) == len(s2):
return True
else:
return False