From 360f04f52115fc30ddb4d5ffea4823533495d084 Mon Sep 17 00:00:00 2001 From: sqlbotler Date: Tue, 2 Jul 2019 23:48:14 -0700 Subject: [PATCH] day10:string permutation check in python --- README.md | 8 ++-- .../1-2-perm-strings.py | 39 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 cracking_the_coding_interview_problems/1-2-perm-strings.py diff --git a/README.md b/README.md index c8ac869..31372ca 100644 --- a/README.md +++ b/README.md @@ -7,10 +7,10 @@ | Current Status| Stats | | :------------: | :----------: | | Total C++ Problems | 188 | -| Total Python Problems | 14 | -| Current Daily Streak| 9 | +| Total Python Problems | 15 | +| Current Daily Streak| 10 | | Last Streak | 06/20/2019 - 06/21/2019| -| Current Streak | 06/23/2019 - 07/01/2019| +| Current Streak | 06/23/2019 - 07/02/2019| @@ -84,7 +84,7 @@ Include contains single header implementation of data structures and some algori | :------------ | :----------: | | Problem 1-1 : Edition 6: Write an algorithm to determine whether a string has unique characters or not. Can we do it without using additional data structures? | [1-1-hasUniqueChars.cpp](cracking_the_coding_interview_problems/1-1-hasUniqueChars.cpp), [1-1-hasUniqueChars.py](cracking_the_coding_interview_problems/1-1-hasUniqueChars.py)| | Problem 1-2 : Edition 5: Reverse a string when you are a pass a null terminated C string.|[1-2-edi5-reverseString.cpp ](cracking_the_coding_interview_problems/1-2-edi5-reverseString.cpp)| -| Problem 1-2 : Edition 6: Given two strings, determine if one is permutation of other.|[1-2-perm-strings.cpp](cracking_the_coding_interview_problems/1-2-perm-strings.cpp)| +| Problem 1-2 : Edition 6: Given two strings, determine if one is permutation of other.|[1-2-perm-strings.cpp](cracking_the_coding_interview_problems/1-2-perm-strings.cpp), [1-2-perm-strings.py](cracking_the_coding_interview_problems/1-2-perm-strings.py)| | Problem 1-3 : Edition 5: Write an algorithm to remove duplicate chars from a string.|[1-3-edi5-removeDuplicates.cpp](cracking_the_coding_interview_problems/1-3-edi5-removeDuplicates.cpp)| | Problem 1-3 : Edition 6: URLify: Replace all the spaces in a string with '%20'. Preferebly Inplace |[1-3-URLify.cpp](cracking_the_coding_interview_problems/1-3-URLify.cpp)| | Problem 1-4 : Edition 6: Given a string, write a function to check if it is a permutation of a pallindrome.|[1-4-pallindrome-permutations.cpp ](cracking_the_coding_interview_problems/1-4-pallindrome-permutations.cpp)| diff --git a/cracking_the_coding_interview_problems/1-2-perm-strings.py b/cracking_the_coding_interview_problems/1-2-perm-strings.py new file mode 100644 index 0000000..1270cfc --- /dev/null +++ b/cracking_the_coding_interview_problems/1-2-perm-strings.py @@ -0,0 +1,39 @@ +""" +Cracking the coding interview edition 6 +Given two strings, determine if one is permutation of other. +""" + +from collections import Counter + + +def check_permutation(str1, str2): + """Checks if the two strings are permutations of each other + Args: + str1 : first string + str2 : second string + Returns: + True if strings are permutations of each other + """ + # if strings are different in size, they can't be permutation of each other. + if len(str1) != len(str2): + return False + + # count the occurance of each char of str1, and match it with str2 + counter = Counter() + for c in str1: + counter[c] += 1 + for c in str2: + if counter[c] == 0: + return False + counter[c] -= 1 + return True + + +if __name__ == "__main__": + str1 = 'listen' + str2 = 'silent' + print(F"'{str1}' is permutation of '{str2}' : ", check_permutation(str1, str2)) + + str1 = 'hello' + str2 = 'world' + print(F"'{str1}' is permutation of '{str2}' : ", check_permutation(str1, str2))