判定是否互为字符重排

作者: _阿南_ | 来源:发表于2020-02-25 20:10 被阅读0次

题目:

给定两个字符串 s1 和 s2,请编写一个程序,确定其中一个字符串的字符重新排列后,能否变成另一个字符串。
示例 1:
输入: s1 = "abc", s2 = "bca"
输出: true 
示例 2:
输入: s1 = "abc", s2 = "bad"
输出: false
说明:
0 <= len(s1) <= 100
0 <= len(s2) <= 100

题目的理解:

两个字符串中的字母相同,那么排列后可以相同。

python实现

class Solution:
    def CheckPermutation(self, s1: str, s2: str) -> bool:
        ascii_amount = 0
        for character in s1:
            ascii_amount += ord(character)
        
        ascii_amount_2 = 0
        for character in s2:
            ascii_amount_2 += ord(character)
            
        return ascii_amount_2 == ascii_amount

提交

还不错哦

// END 啥时候能上班呢,感觉已经影响到我的生活了。

相关文章

网友评论

    本文标题:判定是否互为字符重排

    本文链接:https://www.haomeiwen.com/subject/ysafchtx.html