|
| 1 | +""" |
| 2 | +Python version 3.7.0 |
| 3 | +1.4 - Palindrome Permutation |
| 4 | +Given a string, write a function to check if it is a permutation of a palindrome. |
| 5 | +A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a |
| 6 | +rearrangement of letters. The palindrome does not need to be limited to just dictionary words. |
| 7 | +""" |
| 8 | +import unittest |
| 9 | +import itertools as it |
| 10 | + |
| 11 | + |
| 12 | +def _is_palindrome(word: str) -> bool: |
| 13 | + """ |
| 14 | + Checks if word is a palindrome. |
| 15 | + A palindrome is a word or phrase that is the same forwards and backwards. |
| 16 | + Whitespace will not be considered when determining palindrome. |
| 17 | + This function is case insensitive. |
| 18 | + :param word: the word we check |
| 19 | + :return: true if word is a palindrome, false otherwise |
| 20 | + """ |
| 21 | + word_no_spaces = word.replace(' ', '').lower() |
| 22 | + reversed_word = word_no_spaces[::-1] |
| 23 | + return word_no_spaces == reversed_word |
| 24 | + |
| 25 | + |
| 26 | +def is_permutation_of_palindrome(word: str) -> bool: |
| 27 | + """ |
| 28 | + Given a string, this function will return whether the string is a permutation of a palindrome. |
| 29 | + A palindrome is a word or phrase that is the same forwards and backwards. |
| 30 | + A permutation is a rearrangement of letters. |
| 31 | + When evaluating whether a string is the same forwards and backwards, we will |
| 32 | + omit whitespace. For ex: "taco cat" would not equal "tac ocat" IF we expect a space at the 4th index for |
| 33 | + the reversed version. So, the space will not be taken into account only when determining palindrome. |
| 34 | + Assuming ASCII |
| 35 | + Runtime is O(n!) |
| 36 | + Space complexity is O(n!) |
| 37 | + Given: Expect: |
| 38 | + Tact Coa True (permutations: "taco cat", "atco cta") |
| 39 | +
|
| 40 | + :param word: the string that we want to check for perm of a palindrome |
| 41 | + :return: True if word is a palindrome, False otherwise. |
| 42 | + """ |
| 43 | + word_no_spaces = word.replace(' ', '') |
| 44 | + perms = [''.join(p) for p in it.permutations(word_no_spaces)] |
| 45 | + for p in perms: |
| 46 | + if _is_palindrome(p): |
| 47 | + return True |
| 48 | + return False |
| 49 | + |
| 50 | + |
| 51 | +class TestIsPermutationOfPalindromeFunction(unittest.TestCase): |
| 52 | + def test_is_permutation_of_palindrome(self): |
| 53 | + cases = [ |
| 54 | + ("Tact Coa", True), |
| 55 | + ("car race", True), |
| 56 | + ("ppilffli", True), |
| 57 | + ("gwas", False), |
| 58 | + ("sldkjflksd", False), |
| 59 | + (" ", True), |
| 60 | + ("", True), |
| 61 | + ("a", True) |
| 62 | + ] |
| 63 | + for word, expected in cases: |
| 64 | + self.assertEqual(is_permutation_of_palindrome(word), expected, msg=word) |
| 65 | + |
| 66 | + def test_is_palindrome(self): |
| 67 | + cases = [ |
| 68 | + ("Taco Cat", True), |
| 69 | + ("race car", True), |
| 70 | + ("flippilf", True), |
| 71 | + ("swag", False), |
| 72 | + ("miguel", False), |
| 73 | + (" ", True), |
| 74 | + ("", True), |
| 75 | + ("a", True) |
| 76 | + ] |
| 77 | + for word, expected in cases: |
| 78 | + self.assertEqual(_is_palindrome(word), expected, msg=word) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + unittest.main() |
0 commit comments