add recursive palindrome checker
parent
af47764be0
commit
3338bee2f4
|
@ -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
|
||||
|
|
|
@ -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)
|
Loading…
Reference in New Issue