力扣刷题_多解法:给定两个由小写字母组成的字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。

文章介绍了如何编写一个程序来判断两个给定的小写字母字符串s1和s2,通过字符重排是否能互相转换。提供了多种解决方案,如使用哈希表、排序、计数器等方法来比较字符出现次数。
摘要由CSDN通过智能技术生成
# 给定两个由小写字母组成的字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。 
# 
#  示例 1: 
# 
#  
# 输入: s1 = "abc", s2 = "bca"
# 输出: true 
#  
# 
#  示例 2: 
# 
#  
# 输入: s1 = "abc", s2 = "bad"
# 输出: false
#  
# 
#  说明: 
# 
#  
#  0 <= len(s1) <= 100 
#  0 <= len(s2) <= 100 
#  
# 
#  Related Topics 哈希表 字符串 排序 👍 169 👎 0


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:

    def CheckPermutation1(self, s1: str, s2: str) -> bool :
        if len(s1) != len(s2) :
            return False
    def CheckPermutation2(self,s1,s2):
        if len(s1)!=len(s2):
            return False
        from collections import defaultdict
        dic=defaultdict(int)
        for c in s1:
            dic[c]+=1
        for c in s2:
            dic[c]-=1
        for val in dic.values():
            if val!=0:
                return False
        return True

    def CheckPermutation3(self, s1, s2) :
        if len(s1) != len(s2) :
            return False
        ###对s1和s2进行排序
        sorted_str1=sorted(s1)
        sorted_str2=sorted(s2)
        if sorted_str1==sorted_str2:
            return True
        return False
    def  CheckPermutation4(self, s1, s2) :
        if len(s1) != len(s2) :
            return False
        char_count1={}
        char_count2={}
        for char in s1:
            char_count1[char]=char_count1.get(char,0)+ 1
        for char in s2:
            char_count2[char]=char_count2.get(char,0)+ 1
        return char_count1==char_count2
    def  CheckPermutation5(self, s1, s2) :
        if len(s1) != len(s2) :
            return False
        char_count1={}
        char_count2={}
        for i in range(len(s1)):
            char_count1[s1[i]]=char_count1.get(s1[i],0)+1
            char_count2[s2[i]] = char_count2.get(s2[i],0)+1
        for i in range(len(s1)):
            c=s1[i]
            if c in char_count2:
                if char_count1[c]!=char_count2[c]:
                    return False
            else:
                return False
        return True
    def CheckPermutation6(self, s1, s2) :
        ###TODO:异或
        if len(s1) != len(s2) :
            return False
        flag=0
        sumA=0
        sumB=0
        for i in range(len(s1)):
            sumA+=ord(s1[i])-ord('a')
            sumB+=ord(s2[i])-ord('a')
            flag^= ord(s1[i])-ord('a')
            flag ^= ord(s2[i]) - ord('a')
        if sumA!=sumB:
            return False
        return flag == 0
    def CheckPermutation7(self, s1, s2) :
        from collections import Counter
        if len(s1) != len(s2) :
            return False
        counter1=Counter(s1)
        counter2=Counter(s2)
        return counter1==counter2
    def CheckPermutation8(self, s1: str, s2: str) -> bool :
        # 哈希表
        if len(s1) != len(s2) :
            return False
        set_char = dict()
        for char in s1 :
            if char not in set_char :
                set_char[char] = 1
            else :
                # mark=ord(char)-ord('a')
                set_char[char] += 1
        for char in s2 :
            if char not in set_char :
                return False
            set_char[char] -= 1
        for count in set_char.values() :
            if count > 0 :
                return False
        return True

    def CheckPermutation9(self, s1: str, s2: str) -> bool :
        if not s1 or len(s1)==0 or not s2 or len(s2) == 0:
            return False
        length=len(s1)
        if length !=len(s2):
            return False
        memory=[0]*26
        for i in range(length):
            memory[ord(s1[i]) - ord('a')]+=1
            memory[ord(s2[i]) - ord('a')] -= 1
        for num in memory:
            if num!=0:
                return False
        return True
sol = Solution()
s1 = "abc"
s2 = "bca"
print(sol.CheckPermutation(s1, s2))  # 输出 True
# leetcode submit region end(Prohibit modification and deletion)
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值