Checking if two strings are permutations of each other in Python

前端 未结 22 2446
旧时难觅i
旧时难觅i 2020-12-08 16:04

I\'m checking if two strings a and b are permutations of each other, and I\'m wondering what the ideal way to do this is in Python. From the Zen of

22条回答
  •  既然无缘
    2020-12-08 16:06

    def matchPermutation(s1, s2):
      a = []
      b = []
    
      if len(s1) != len(s2):
        print 'length should be the same'
      return
    
      for i in range(len(s1)):
        a.append(s1[i])
    
      for i in range(len(s2)):
        b.append(s2[i])
    
      if set(a) == set(b):
        print 'Permutation of each other'
      else:
        print 'Not a permutation of each other'
      return
    
    #matchPermutaion('rav', 'var') #returns True
    matchPermutaion('rav', 'abc') #returns False
    

提交回复
热议问题