给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = "abc", s2 = "bca"
输出: true
示例 2:
输入: s1 = "abc", s2 = "bad"
输出: false
# 判定是否互为字符重拍
class Solution:
def CheckPermutation(self, s1: str, s2: str) -> bool:
#创建两个dict 字典
sdict1 = {}
sdict2 = {}
for i in s1:
if i not in sdict1.keys():
sdict1[i] = 0
else:
sdict1[i] += 1
for i in s2:
if i not in sdict2.keys():
sdict2[i] = 0
else:
sdict2[i] += 1
if len(sdict1) != len(sdict2):
return False
return sdict1==sdict2 #验证两个字典是否相同,直接使用==就可以
if __name__ == '__main__':
a = Solution()
print(a.CheckPermutation("abc", "bda"))