Sunday, 18 July 2021

Palindrome Check using Recursion

 Problem: 

You are given a String 'S'. You need to recursively find the if the string is Palindrome or not.

Test case 1: 

Input: abba

Output: True

Test case 2: 

Input: abbcbba

Output: True

Test case 3: 

Input: abbad

Output: False

Source code:


import java.util.*;

import java.io.*;



public class Main

{

    static int k=1;

public static void main(String[] args) 

{

    Scanner s=new Scanner(System.in);

    System.out.println("Enter String");

    String str =s.next();

System.out.println("Answer: "+isPalindrome(str,0,str.length()-1));

}

public static boolean isPalindrome(String str,int start,int end)

{

    if(start>=end)

        return true;

        

    return ((str.charAt(start)==str.charAt(end)) && isPalindrome(str,start+1,end-1));

}

}





 

No comments:

Post a Comment

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...