Skip to content

Commit 360f04f

Browse files
committed
day10:string permutation check in python
1 parent 3acb1dd commit 360f04f

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
| Current Status| Stats |
88
| :------------: | :----------: |
99
| Total C++ Problems | 188 |
10-
| Total Python Problems | 14 |
11-
| Current Daily Streak| 9 |
10+
| Total Python Problems | 15 |
11+
| Current Daily Streak| 10 |
1212
| Last Streak | 06/20/2019 - 06/21/2019|
13-
| Current Streak | 06/23/2019 - 07/01/2019|
13+
| Current Streak | 06/23/2019 - 07/02/2019|
1414

1515
</center>
1616

@@ -84,7 +84,7 @@ Include contains single header implementation of data structures and some algori
8484
| :------------ | :----------: |
8585
| 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)|
8686
| 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)|
87-
| 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)|
87+
| 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)|
8888
| 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)|
8989
| 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)|
9090
| 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)|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Cracking the coding interview edition 6
3+
Given two strings, determine if one is permutation of other.
4+
"""
5+
6+
from collections import Counter
7+
8+
9+
def check_permutation(str1, str2):
10+
"""Checks if the two strings are permutations of each other
11+
Args:
12+
str1 : first string
13+
str2 : second string
14+
Returns:
15+
True if strings are permutations of each other
16+
"""
17+
# if strings are different in size, they can't be permutation of each other.
18+
if len(str1) != len(str2):
19+
return False
20+
21+
# count the occurance of each char of str1, and match it with str2
22+
counter = Counter()
23+
for c in str1:
24+
counter[c] += 1
25+
for c in str2:
26+
if counter[c] == 0:
27+
return False
28+
counter[c] -= 1
29+
return True
30+
31+
32+
if __name__ == "__main__":
33+
str1 = 'listen'
34+
str2 = 'silent'
35+
print(F"'{str1}' is permutation of '{str2}' : ", check_permutation(str1, str2))
36+
37+
str1 = 'hello'
38+
str2 = 'world'
39+
print(F"'{str1}' is permutation of '{str2}' : ", check_permutation(str1, str2))

0 commit comments

Comments
 (0)