Skip to content

Instantly share code, notes, and snippets.

@xiaotdl
Created April 4, 2015 08:32
Show Gist options
  • Save xiaotdl/44950ae0ff5830913447 to your computer and use it in GitHub Desktop.
Save xiaotdl/44950ae0ff5830913447 to your computer and use it in GitHub Desktop.
"""
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