LeetCode 面试题 01.02. 判定是否互为字符重排

题目

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

示例 1:

输入: s1 = "abc", s2 = "bca"
输出: true 
示例 2:

输入: s1 = "abc", s2 = "bad"
输出: false

说明:

0 <= len(s1) <= 100
0 <= len(s2) <= 100

解题思路

遍历各数组1遍,O(n)

class Solution:
    def CheckPermutation(self, s1: str, s2: str) -> bool:
        aList = [i for i in s1]
        for j in s2:
            try:
                aList.remove(j)
            except:
                return False
        return True
上一篇:LeetCode 面试题 02.01. 移除重复节点


下一篇:LeetCode 面试题 01.06. 字符串压缩