25. Palindrome String
The problem can be found at the following link: Question Link
My Approach
I have implemented a two-pointer approach.
I start with two pointers, one at the beginning of the string (
i
) and one at the end of the string (j
).I iterate through the string, comparing characters at these two pointers. If they ever differ, I return
false
as it's not a palindrome.If I successfully reach the middle of the string without finding any differing characters, I return
true
.
Time and Auxiliary Space Complexity
Time Complexity:
O(n)
, wheren
is the length of the input stringS
. We traverse half of the string to determine if it's a palindrome.Auxiliary Space Complexity:
O(1)
, as we are using a constant amount of extra space to storei
andj
.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the getlost01/gfg-potd repository.
Last updated