forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
day10:string permutation check in python
- Loading branch information
Showing
2 changed files
with
43 additions
and
4 deletions.
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
cracking_the_coding_interview_problems/1-2-perm-strings.py
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
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)) |