给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = "abc", s2 = "bca"
输出: true
示例 2:
输入: s1 = "abc", s2 = "bad"
输出: false
说明:
- 0 <= len(s1) <= 100
- 0 <= len(s2) <= 100
Python 解答:
1.统计数字
class Solution:
def CheckPermutation(self, s1: str, s2: str) -> bool:
if len(s1) != len(s2):
return False
adic = {}
for s in s1:
if s not in adic.keys():
adic[s] = 1
else:
adic[s] += 1
for s in s2:
if s not in adic.keys():
return False
else:
adic[s] -= 1
if adic[s] < 0:
return False
return True
2.排序
class Solution:
def CheckPermutation(self, s1: str, s2: str) -> bool:
return sorted(s1) == sorted(s2)
留言