Advertisement
Guest User

Untitled

a guest
May 26th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #1.2 Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
  2.  
  3. def check_perm(str1, str2):
  4. hash1 = {}
  5. hash2 = {}
  6. for val in str1:
  7. if val not in hash1:
  8. hash1[val] = 0
  9. hash1[val] += 1
  10.  
  11. for val in str2:
  12. if val not in hash2:
  13. hash2[val] = 0
  14. hash2[val] += 1
  15.  
  16. for val in hash1:
  17. if val not in hash2 or hash1[val] != hash2[val]:
  18. return False
  19.  
  20. for val in hash2:
  21. if val not in hash1 or hash2[val] != hash1[val]:
  22. return False
  23.  
  24. return True
  25.  
  26.  
  27. print(1 if check_perm("a", "a") == True else 0)
  28. print(1 if check_perm("aa", "a") == False else 0)
  29. print(1 if check_perm("", "a") == False else 0)
  30. print(1 if check_perm("acdefg", "gfedca") == True else 0)
  31. print(1 if check_perm("acdefggf", "gfedca") == False else 0)
  32. print(1 if check_perm("aabb", "aabb") == True else 0)
  33. print(1 if check_perm("bb", "aabb") == False else 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement