Check If a String Reads the Same Forwards and Backwards — Recursively
Implement isPalindromeRecursive
Given a lowercase string
s, determine whether it reads the same forwards and backwards.
The two-pointer idea — compare the outer characters, then move inward — translates directly into recursion: each call handles exactly one pair, and if that pair matches, the rest of the question ("does everything strictly between them also form a palindrome?") is answered by recursing on the smaller window one step in from each side. The base case, where the two pointers meet or cross, means every pair along the way matched successfully, so the answer is true. The recursive version does the exact same comparisons as the iterative one — it just spends a stack frame per pair instead of a single loop variable.
Example 1:
Input: s = "deified"
Output: true
Example 2:
Input: s = "hello"
Output: false
Example 3:
Input: s = "stats"
Output: true
+ 3 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ s.length ≤ 15 - ●
s consists of lowercase English letters only
s =
deified