Skip to content

Commit

Permalink
day10:string permutation check in python
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Jul 3, 2019
1 parent 3acb1dd commit 360f04f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|

</center>

Expand Down Expand Up @@ -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)|
Expand Down
39 changes: 39 additions & 0 deletions cracking_the_coding_interview_problems/1-2-perm-strings.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 360f04f

Please sign in to comment.