
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a String is Palindrome Using Two Pointer in C++
A palindrome is a word, phrase, number, or other sequence of characters that reads the same from both backward and forward sides. In strings, the palindrome strings are those strings that can be read the same from both left and right sides. In this article, we are going to learn how we can check if a given string is palindrome or not using the two-pointer technique in C++.
Problem
We are given a string, and we have to check if it is palindrome or not using the two-pointer method in C++.
Example 1
Input: "abcba"
Output: True
Explanation
"abcba" is a palindrome because it reads the same forward and backwards and result same or identical string when reversed and compare to original one.
Example 2
Input: "ayush"
Output: False
Explanation
"ayush" is not a palindrome because it doesn't reads the same forward and backwards and doesn't result same or identical string when reversed and compare to original one.
Example 3
Input: "Abcba"
Output: False
Explanation
"Abcba" is not a palindrome since it doesn't read the same forward and backward, and this comparison is case-sensitive unless you explicitly ignore case differences.
Solution by Using Two Pointers
The two-pointer technique is a popular algorithm used to solve problems that involve sequences, like arrays or strings. It is one of the approaches that helps in reducing time complexity and making the solution more efficient and easy to understand. In this method, we use two pointers: one from the start and the other from the end. We set the left pointer at the start of the string (index 0) and the right pointer at the end of the string (length - 1). We compare the characters at the left and right positions until the left pointer is no longer less than the right pointer. If at any position the characters are not equal, we return false. If they are equal, we move the left pointer one position forward and the right pointer one position backward.
Steps
- Set up two pointers: left and right. The left starts at the beginning of the string (index 0) and the right starts at the end of the string (length - 1).
- Run the loop until left is less than right. If the characters are equal, move the left pointer one position forward and the right pointer one position backward. Continue this process until the left pointer meets or passes the right pointer.
- Return true after the loop completes.
Implementation
#include <bits/stdc++.h> using namespace std; bool stringPalindrome(string str) { int left = 0; int right = str.size() - 1; while (left < right) { if (str[left] != str[right]) { return false; } left++; right--; } return true; } int main() { string input; cout << "Enter a string: "<< endl; cin >> input; if (stringPalindrome(input)) { cout << "The string is a palindrome." << endl; } else { cout << "The string is not a palindrome." << endl; } return 0; }
Output:
Enter a string:
aba
The string is a palindrome.
Time Complexity: O(n), as we are traversing half the string.
Space Complexity: O(1), constant space