add recursive palindrome checker

pull/1184/head
Lily Sam 2023-05-03 22:19:38 -04:00
parent af47764be0
commit 3338bee2f4
2 changed files with 21 additions and 0 deletions

View File

@ -29,6 +29,7 @@
- [Recursive Insertion Sort](recursion/recursive_insertion_sort.py)
- [Recursive Sum of n numbers](recursion/recursive-sum-of-n-numbers.py)
- [GCD by Euclid's Algorithm](recursion/gcd_using_recursion.py)
- [Recursive Is-Palindrome](recursion/recursive_palindrome.py)
## Scheduling

View File

@ -0,0 +1,20 @@
"""
Function that uses a recursive approach to check
if a string is a palindrome
Time Complexity: O(N) where N is the length of the
string
"""
def recursive_is_palindrome(s: str) -> bool:
if len(s) <= 1:
return True
if s[0] != s[-1]:
return False
return recursive_is_palindrome(s[1:-1])
# test cases
print(recursive_is_palindrome("abba") is True)
print(recursive_is_palindrome("abba") is True)
print(recursive_is_palindrome("") is True)
print(recursive_is_palindrome("abcd") is False)