Leetcode刷题记录——面试题 01.02. 判定是否互为字符重排
程序员文章站
2024-03-04 09:24:35
...
我们先用一个字典储存s1中的全部字符和出现次数
再遍历s2,从字典中的对应位置减
class Solution:
def CheckPermutation(self, s1: str, s2: str) -> bool:
s1dict = {}
for letter in s1:
if letter in s1dict:
s1dict[letter] += 1
else:
s1dict[letter] = 1
for letter in s2:
if letter not in s1dict or s1dict[letter] == 0:
return False
else:
s1dict[letter] -= 1
return True
上一篇: sprng和struts有什么区别?