Created
April 4, 2015 08:32
-
-
Save xiaotdl/44950ae0ff5830913447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
1.3 Given two strings, write a method to decide if one is a permutation of the other. | |
tips: | |
assumption - case sensitive, whitespace matters | |
""" | |
# sort string and compare | |
# Complexity: time O(nlogn), space O(1) | |
def is_permutation(str1, str2): | |
if not str1 or not str2 or len(str1) != len(str2): | |
return False | |
return ''.join(sorted(str1)) == ''.join(sorted(str2)) | |
def main(): | |
s1 = "abc" | |
s2 = "cba" | |
s3 = "aaa" | |
print is_permutation(s1, s2) == True | |
print is_permutation(s1, s3) == False | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment