String Permutation

云计算 waitig 506℃ 百度已收录 0评论

Given two strings, write a method to decide if one is a permutation of the other.

Have you met this question in a real interview? 

Yes

python

class Solution:
    """
    @param: A: a string
    @param: B: a string
    @return: a boolean
    """
    def Permutation(self, A, B):
        # write your code here
        if A is None and B is not None:
            return False
        if A is not None and B is None:
            return False
        if A is None and B is None:
            return True
        if len(A) != len(B):
            return False
        arr = [0] * 256
        for ele in list(A):
            arr[ord(ele)] += 1
        for val in list(B):
            arr[ord(val)] -= 1
            if arr[ord(val)] < 0:
                return False
        return True

本文由【waitig】发表在等英博客
本文固定链接:String Permutation
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)