class Solution {
public:
bool CheckPermutation(string s1, string s2) {
}
};
面试题 01.02. 判定是否互为字符重排
给定两个字符串 s1
和 s2
,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入:s1
= "abc",s2
= "bca" 输出: true
示例 2:
输入:s1
= "abc",s2
= "bad" 输出: false
说明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100
原站题解
python3 解法, 执行用时: 44 ms, 内存消耗: 13.2 MB, 提交时间: 2020-09-09 21:18:15
class Solution: def CheckPermutation(self, s1: str, s2: str) -> bool: if len(s1) != len(s2): return False for c in s1: s2 = s2.replace(c, '', 1) return s2 == ''
python3 解法, 执行用时: 32 ms, 内存消耗: 13.3 MB, 提交时间: 2020-09-09 21:13:36
class Solution: def CheckPermutation(self, s1: str, s2: str) -> bool: if len(s1) != len(s2): return False s3 = [c for c in s2] for i in s1: if i in s3: s3.remove(i) return len(s3) == 0