Check string is Palindrome

 

Recursive Technique


public class RecursionPalindrome {

public static void main(String[] args) {
String str="madada";
System.out.println(new RecursionPalindrome().recursivePalindrome(str,0,str.length()-1));
}

public boolean recursivePalindrome(String txt,int front,int back){
if(front==back)
return true;
if(txt.charAt(front)!=txt.charAt(back))
return false;
if(front<back+1)
return recursivePalindrome(txt,front+1,back-1);
return false;
}
}


Using Loop



public class Palindrome {

public boolean isPalindrome(String str){
int l=str.length()-1;
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)!=str.charAt(l-i))
return false;
}
return true;
}

public static void main(String[] args) {
System.out.println(new Palindrome().isPalindrome("malayala"));
}
}

Comments

Popular posts from this blog

Get first non-repeating Letter

Cypress run command

Docker commands