A special site for solving fun programming problems and challenges, interested in computer science, programming, basics, data structure and algorithms

 


Given a string S, check if it is palindrome or not.

Example 1:

Input: S = "abba"
Output: 1
Explanation: S is a palindrome

Example 2:

Input: S = "abc" 
Output: 0
Explanation: S is not a palindrome

 

Your Task:
You don't need to read input or print anything. Complete the function isPlaindrome()which accepts string S and returns an integervalue 1 or 0.


Expected Time Complexity: O(Length of S)
Expected Auxiliary Space: O(1)


Constraints:
1 <= Length of S<= 105



solution<1>

class Solution{
public:	
	
	
	int isPalindrome(string S)
	{
	    // Your code goes here
	    int n=S.length();
        string ans;
    
        for(int i=n-1;i>=0;i--)
         {
             ans+=S[i];
         }
         
        if(S.compare(ans)==0)
            return 1;
            
        return 0;
	}

};

 solution<2>

class Solution{
public:	
	
	
	int isPalindrome(string S)
	{
	    // Your code goes here
        string ans;
    
        for(int i=S.length()-1;i>=0;i--)
         {
             ans+=S[i];
         }
         
        if(S==ans)
            return 1;
            
        return 0;
	}

};

 solution<3>

class Solution{
public:	
	
	
	int isPalindrome(string S)
	{
	    // Your code goes here
        int n=S.length();
        for(int i=0;i<n/2;i++)
        {
            if(S[i]!=S[n-1-i])
            {
                return 0;
            }
        }
        return 1;
	}

};

No comments:

Post a Comment