题目

给定两个字符串s1s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
image.png

思路

本质上就是判断s1中的字符类别和数目是否和s2中一致。

  1. class Solution:
  2. def CheckPermutation(self, s1: str, s2: str) -> bool:
  3. s1_ = dict()
  4. s2_ = dict()
  5. for ch in s1:
  6. s1_[ch] = s1_.get(ch, 0) + 1
  7. for ch in s2:
  8. s2_[ch] = s2_.get(ch, 0) + 1
  9. return s1_ == s2_