|
| 1 | +""" |
| 2 | +Python version 3.7.0 |
| 3 | +1.2 - Check Permutation |
| 4 | +Given two strings, write a method to decide if one is a permutation of the other. |
| 5 | +""" |
| 6 | +import unittest |
| 7 | +from typing import Callable |
| 8 | + |
| 9 | + |
| 10 | +def check_permutation(s1: str, s2: str) -> bool: |
| 11 | + """ |
| 12 | + Determines if a s1 is a permutation of s2. |
| 13 | + s1 is a permutation of s2 if the characters from s1 map to |
| 14 | + the same characters in s2 in a one-to-one correspondence AND s1 and s2 are of the same length. |
| 15 | + Runtime: O(n + m), where n is the length of s1 and m is the length of s2. |
| 16 | + Given: Expect: |
| 17 | + "tacos", "costa" True |
| 18 | + "tacoos", "costa" False |
| 19 | + "swag", "hello" False |
| 20 | + "hi", "hello" False |
| 21 | + "hola", "loha" True |
| 22 | + "p3nding", "dingp3n" True |
| 23 | + "", "test" False |
| 24 | + "", "" True |
| 25 | + "test2", "" False |
| 26 | + :param s1: string of size n |
| 27 | + :param s2: string of size m |
| 28 | + :return: True if s1 is a permutation of s2, False otherwise |
| 29 | + """ |
| 30 | + # permutations must be of equal length, one-to-one |
| 31 | + if len(s1) != len(s2): |
| 32 | + return False |
| 33 | + freqs_s1 = {} |
| 34 | + # build histogram of seen characters in s1 |
| 35 | + # using histogram because string could have repeated characters |
| 36 | + for c in s1: |
| 37 | + if c not in freqs_s1: |
| 38 | + freqs_s1[c] = 1 |
| 39 | + continue |
| 40 | + freqs_s1[c] += 1 |
| 41 | + |
| 42 | + freqs_s2 = {} |
| 43 | + for c in s2: |
| 44 | + # if character in s2 not in s1, then this is not a permutation |
| 45 | + if c not in freqs_s1: |
| 46 | + return False |
| 47 | + # otherwise, populate as normal |
| 48 | + if c not in freqs_s2: |
| 49 | + freqs_s2[c] = 1 |
| 50 | + continue |
| 51 | + freqs_s2[c] += 1 |
| 52 | + |
| 53 | + # compare frequences of characters for s1 and s2 |
| 54 | + for key, val in freqs_s1.items(): |
| 55 | + if val != freqs_s2[key]: |
| 56 | + # character counts between s1 and s2 don't match |
| 57 | + return False |
| 58 | + return True |
| 59 | + |
| 60 | + |
| 61 | +class TestCheckPermutationFunction(unittest.TestCase): |
| 62 | + def _run_tests(self, f: Callable[[str, str], None]) -> None: |
| 63 | + false_cases = [ |
| 64 | + ("tacoos", "costa"), ("swag", "hello"), ("hi", "hello"), ("tacooos", "costaaa"), |
| 65 | + ("", "test"), ("test2", "") |
| 66 | + ] |
| 67 | + for case in false_cases: |
| 68 | + self.assertFalse(f(case[0], case[1]), msg=case) |
| 69 | + |
| 70 | + true_cases = [ |
| 71 | + ("tacos", "costa"), ("hola", "loha"), ("p3nding", "dingp3n"), ("same", "same"), ("", "") |
| 72 | + ] |
| 73 | + for case in true_cases: |
| 74 | + self.assertTrue(f(case[0], case[1]), msg=case) |
| 75 | + with self.assertRaises(TypeError): |
| 76 | + f(8) |
| 77 | + |
| 78 | + def test_check_permutation(self): |
| 79 | + self._run_tests(check_permutation) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + unittest.main() |
0 commit comments