Interesting problem with an interesting approach. Well, the problem statement is regular but the approach is nice.
We iterate over half the string and check if characters at the opposite ends are equal. If so, we shrink the window.
If we come across a mismatch, we divide the string into two parts. From start
to i
, and from i
to end
.
Now, our solution need to make a several choices.
A) Either both substrings are palindromes, in which case all is good. That was the single mismatch in the entire string, which is allowed.
B) Either left substring is a palindrome and the right is not. In this case, it works too - because we can allow only one mismatch - which is in the right partition.
C) Either right substring is a palindrome and the left is not. This works because the same logic as point B.
D) Both are not palindromes, this case does not work obviously because we are allowed only one mismatch throughout the string, and now there are two mismatches, so we return False
.
Code:
Python
Big O Analysis
-
Runtime
The runtime complexity here is .
-
Memory
Constant space, we aren’t using any extra data structures.
— A