Palindrome String Geeks For Geeks (2 Different Approaches)

First Approach 


Using StringBuffer Reverse method

Time : 0.3 sec / 11.1 sec

 



class Solution {

    int isPlaindrome(String s) {

        

StringBuffer sb=new StringBuffer();

sb.append(s);

String a=(sb.reverse()).toString();

if(a.equals(s)){

//System.out.println("True");

return 1;

}

else{

//System.out.println("False");

return 0;

}

        

        

        

        

        // code here

    }

};



##################################################################################


Another Approach 



Comparing each character

Time : 0.3 sec / 11.1 sec


class Solution {

    int isPlaindrome(String s) {

                int start=0;

int count=0;

int end=s.length()-1;

if(s.length()>1){

while(start<end){

char a=s.charAt(start);

char b=s.charAt(end);

if(a==b){

start++;

end--;

count++;

}

else

{

count=0;

break;

}

}

if(count!=0){

//System.out.println("Yes");

return 1;

}

else

// System.out.println("No");

return 0;

}

else{

//System.out.println("Yes");

return 1;

}

        

        

        // code here

    }

};


Hope It Helped You.

Thanks for giving it a read.😇

Comments

Popular posts from this blog

Solutions Of Practice Questions Dated 01-06-2022

CODEFORCES SPY DETECTED ROUND 713

Maximum Winning Score Geeks For Geeks