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
Post a Comment