Let's see how we will implement it through code. At first, we will check if our string is of length 1
or not. If it has a length of 1
, then we don't have to proceed, as you know that a single character is a palindrome by itself.
Then we can check if our string is of length 2
or not. We are checking these specific lengths to avoid extra work, as our function doesn't have to execute all the awy. Adding these checks will improve the method's performance.
What will happen if we pass the string "Maham"
?