Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › RECURSION BASICS
For the string "abca", the first and last characters are at indices 0 and 3. They are both 'a', so the outer pair matches. That does not settle the question, because a palindrome must also match every pair moving inward.
string: a b c a
index: 0 1 2 3
pair 1: index 0 and 3 -> 'a' == 'a'
pair 2: index 1 and 2 -> 'b' != 'c'The mirrored pairs in "abca" are indices (0, 3) and (1, 2). The pair (0, 3) can allow the check to continue, but it cannot return true by itself. The pair (1, 2) contains 'b' and 'c', which differ, so the complete string is not a palindrome.
Can "abca" be declared a palindrome after comparing only indices 0 and 3?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Use two indices to describe the part of "abca" that has not been settled. The first call checks left = 0 and right = 3. If those characters match, neither needs checking again, so the next call checks left = 1 and right = 2.
bool isPalindrome(const string& s, int left, int right) {
if (left >= right) return true;
if (s[left] != s[right]) return false;
return isPalindrome(s, left + 1, right - 1);
}The condition left >= right is the base case. It means the unchecked range has one character or no characters, so every required pair before it matched. For "abca", the state changes from (0, 3) to (1, 2), and the second comparison finds the mismatch.
When the call for (0, 3) sees that 'a' equals 'a', it pauses while the call for (1, 2) runs. The inner call sees that 'b' does not equal 'c', so it returns false immediately. There is no need to inspect any further pair.
The waiting call for (0, 3) must return the result it receives from (1, 2). That false value travels back through the call stack and becomes the result for the whole string. If the outer call simply returned true after seeing equal 'a' characters, it would hide the inner mismatch.
// State (0, 3)
'a' == 'a'
// wait for the recursive call
// State (1, 2)
'b' != 'c'
return false;
// State (0, 3) receives false
return false;Replace the faulty return so the matching pair at (0, 3) passes the inner result back to the caller.
if (s[left] == s[right]) {
return true;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A call on a remaining range of m characters compares its two ends in constant time, then removes those two characters from future work. Its recurrence is T(m) = T(m - 2) + O(1). For "abca", the calls compare (0, 3) and (1, 2), so there are at most n/2 pair checks.
Removing two characters per call still creates a number of calls proportional to n. Therefore the time complexity is O(n). The calls wait for their inner calls to return, so the call stack also grows to O(n) in the general bound, more precisely O(n/2), which is the same asymptotic space complexity.
| STATE | COMPARISON | RESULT |
|---|---|---|
| (0, 3) | 'a' == 'a' | Wait for (1, 2) |
| (1, 2) | 'b' != 'c' | false |
| (0, 3) after return | Receives false | false |