Skip to content

Commit

Permalink
feat(strings): check permutations with map/dict
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Dec 24, 2024
1 parent 2e13619 commit 315abad
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
54 changes: 53 additions & 1 deletion pystrings/permutation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
def check_permutation(input_str_1: str, input_str_2: str) -> bool:
from typing import Dict


def check_permutation_with_sorting(input_str_1: str, input_str_2: str) -> bool:
"""
Check if two strings are permutations of each other.
Expand Down Expand Up @@ -32,3 +35,52 @@ def check_permutation(input_str_1: str, input_str_2: str) -> bool:
return False

return True

def check_permutation_with_map(input_str_1: str, input_str_2: str) -> bool:
"""
Check if two strings are permutations of each other.
This function determines whether two input strings are permutations of one
another by comparing the frequency of characters in each string. It considers
case sensitivity and whitespace as significant when evaluating the two strings.
Complexity:
Time: O(n) as iteration is handled on each string where n is the number of characters in each string
Space: O(n) as a dictionary is used to store characters for lookups
Args:
input_str_1 (str): The first input string to compare.
input_str_2 (str): The second input string to compare.
Returns:
bool
True if the two strings are permutations of each other, False otherwise.
"""
# strings of different lengths can not be permutations of each other
if len(input_str_1) != len(input_str_2):
return False

input_str_1_lower = input_str_1.lower()
input_str_2_lower = input_str_2.lower()

char_count: Dict[str, int] = dict()

# Now, if input_str_1 and input_str_2 are permutations of each other, the two for loops will counterbalance each
# other and the values of all the keys in char_count should be 0.
# We find this out at the end where we evaluate count == 0 for all the keys in char_count. Using the all function,
# we combine the evaluation results from all the iterations and return it from the function. The all() function
# returns True if all items in an iterable are True. Otherwise, it returns False.

for char in input_str_1_lower:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1

for char in input_str_2_lower:
if char in char_count:
char_count[char] -= 1
else:
return False

return all(count == 0 for count in char_count.values())
24 changes: 20 additions & 4 deletions pystrings/permutation/test_check_permutation.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import unittest
from . import check_permutation
from . import check_permutation_with_sorting, check_permutation_with_map


class CheckPermutationTestCases(unittest.TestCase):
class CheckPermutationWithSortingTestCases(unittest.TestCase):
def test_1(self):
"""should return True for 'google' and 'ooggle'"""
word_1 = "google"
word_2 = "ooggle"
actual = check_permutation(word_1, word_2)
actual = check_permutation_with_sorting(word_1, word_2)
self.assertTrue(actual)

def test_2(self):
"""should return False for 'not' and 'top'"""
word_1 = "not"
word_2 = "top"
actual = check_permutation(word_1, word_2)
actual = check_permutation_with_sorting(word_1, word_2)
self.assertFalse(actual)


class CheckPermutationWithMapTestCases(unittest.TestCase):
def test_1(self):
"""should return True for 'google' and 'ooggle'"""
word_1 = "google"
word_2 = "ooggle"
actual = check_permutation_with_map(word_1, word_2)
self.assertTrue(actual)

def test_2(self):
"""should return False for 'not' and 'top'"""
word_1 = "not"
word_2 = "top"
actual = check_permutation_with_map(word_1, word_2)
self.assertFalse(actual)

if __name__ == '__main__':
Expand Down

0 comments on commit 315abad

Please sign in to comment.