Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Check Palindrome

Easy
0/40
profile
Contributed by
72 upvotes
Asked in companies
EXL ServiceThalesOptum

Problem statement

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.


For example:
‘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.
Detailed explanation ( Input/output format, Notes, Images )

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.


Note
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
Sample Input 1 :
2
racecar
niinja


Sample Output 1 :
1
0


Explanation For Sample Input 1 :
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.


Sample Input 2 :
2
level
panama


Sample Output 2 :
1
0
Hint

Check each element from the first half to its corresponding element in the second half.

Approaches (1)
1
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Check Palindrome
All tags
Sort by
Search icon

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;

}

88 views
0 replies
0 upvotes

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;

}

22 views
0 replies
1 upvote

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;  
}	
29 views
0 replies
0 upvotes

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;

}

17 views
0 replies
0 upvotes

Interview problems

Easy for C++

Tim complexityO(1)

108 views
0 replies
0 upvotes

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 ?

114 views
1 reply
0 upvotes

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;

}

167 views
0 replies
1 upvote

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;
}
69 views
0 replies
0 upvotes

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;
    }

}
105 views
0 replies
1 upvote

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;

}

102 views
0 replies
1 upvote
Full screen
Console