bool isPalindrome(string &s){
int n = s.length();
int i = 0;
int j = n - 1;
while (i < j) {
if (s[i] != s[j]) {
return false;
}
i++;
j--;
}
return true;
}
Problem of the day
You're given an alphabetical string ‘S’.
Determine whether it is palindrome or not. A palindrome is a string that is equal to itself upon reversing it.
‘S’ = racecar
The reverse of ‘S’ is: racecar
Since ‘S’ is equal to its reverse. So ‘S’ is a palindrome.
Hence output will be 1.
Input Format:
The first line of the input contains a single integer ‘T’ representing the no. of test cases.
The first line of each test case contains a single alphabetical string, ‘S’.
Output Format:
For each test case, print a single integer value 1 if the given string ‘S’ is palindrome and 0 otherwise.
Print a separate line for each test case.
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
Constraints -
1 ≤ T ≤ 1000
1 ≤ |S| ≤ 10^5
S consists of only lowercase english alphabets.
Σ|S| ≤ 2 * 10^6
Time limit: 1 Sec
2
racecar
niinja
1
0
For First Case - Same as explained in above example.
For the second case -
‘S’ = niinja
Reverse of ‘S’ is: ajniin
Since ‘S’ is not equal to its reverse. So ‘S’ is not a palindrome.
Hence output will be 0.
2
level
panama
1
0
Check each element from the first half to its corresponding element in the second half.
Interview problems
C++ Code || Simple Code of O(N)
bool isPalindrome(string &s){
int n = s.length();
int i = 0;
int j = n - 1;
while (i < j) {
if (s[i] != s[j]) {
return false;
}
i++;
j--;
}
return true;
}
Interview problems
c++ easy solution
bool isPalindrome(string &s)
{
// Write your code here.
int i=0,j=s.length()-1;
while(i<j){
if (s[i] == s[j]) {
i++;
j--;
}
else
return false;
}
return true;
}
Interview problems
Easy C++ Code || isPalindrome || 🔥🔥🔥||
#include <bits/stdc++.h>
bool isPalindrome(string &s)
{
// Write your code here.
string str = s;
reverse(str.begin(), str.end());
return str == s;
} Interview problems
c++ solution
bool isPalindrome(string &s)
{
// Write your code here.
int st=0;
int e=s.size()-1;
while(st<=e){
if(s[st]!=s[e]){
return 0;
}
else{
st++;
e--;
}
}
return 1;
}
Interview problems
palindrome using recursion ..
bool palindrome(string name , int i , int j){
if( i > j){
return true;
}
if(name[i] != name[j]){
return false;
}
else{
palindrome(name , i +1 , j-1 );
}
}
bool isPalindrome(string &s)
{
bool check = palindrome(s , 0 , s.length()-1);
if(check){
return 1;
}
else{
return 0;
}
}
its pass all case but 1 case it give wrong answer why ? give any suggestion ?
Interview problems
EASY C++ TO CHECK PALINDROME
bool isPalindrome(string &s)
{
int n=s.length()-1;
int st=0;
int e=n;
while(st<=e){
if(tolower( s[st] ) != tolower( s[e] ) ) {
return 0;
}
else{
st++;
e--;
}
}
return 1;
}
Interview problems
C++ SOLUTION EASY
bool isPalindrome(string &s)
{
int len = s.length();
for(int i = 0; i < len/2; i++)
if(s[i] != s[len-i-1]) return false;
return true;
}Interview problems
EASY_JAVA SOL
public class Solution {
public static Boolean isPalindrome(String s) {
// Write your code here..
//Method 1:
// String str="";
// for(int i=s.length()-1 ; i>=0;i--)
// {
// str+=s.charAt(i);
// }
// if(str.equals(s))
// {
// return true;
// }
// else
// return false;
//Method 2
int i=0;
int j=s.length()-1;
while(i<=j)
{
if(s.charAt(i)==s.charAt(j))
{
i++;
j--;
}
else
return false;
}
return true;
}
}Interview problems
ESAY c++
bool isPalindrome(string &s)
{
int i=0,j=s.length()-1;
while(i<=j)
{
if(s[i]!=s[j])
{
return false;
}
i++,j--;
}
return true;
}