Created
May 29, 2018 03:18
-
-
Save thmain/0fc89cbfb744a205bd7b88c66a21392b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Use recursive approach | |
//Compare first and last characters if they are not same- return false | |
//If they are same make, remove the first and last characters and make a recursive call. | |
public class Palindrome { | |
public Boolean isPalindrome(String strX) { | |
if (strX.length() < 2) | |
return true; | |
if (strX.charAt(0) == strX.charAt(strX.length() - 1)) { | |
return isPalindrome(strX.substring(1, strX.length() - 1)); | |
} else | |
return false; | |
} | |
public static void main(String args[]) { | |
String S1 = "Sumit"; | |
String S2 = "SumuS"; | |
String S3 = "ABCDEFGHGFEDCBA"; | |
String S4 = "Jain niaJ"; | |
String S5 = "JasasddfwfcsJ"; | |
Palindrome p = new Palindrome(); | |
System.out.println("Is " + S1 + " Palindrome ??? :" | |
+ p.isPalindrome(S1)); | |
System.out.println("Is " + S2 + " Palindrome ??? :" | |
+ p.isPalindrome(S2)); | |
System.out.println("Is " + S3 + " Palindrome ??? :" | |
+ p.isPalindrome(S3)); | |
System.out.println("Is " + S4 + " Palindrome ??? :" | |
+ p.isPalindrome(S4)); | |
System.out.println("Is " + S5 + " Palindrome ??? :" | |
+ p.isPalindrome(S5)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment