- public class PalindromeUsingRecursionAlgorithm {
- public static void main(String[] args) {
- String s = "ABCBAd";
- boolean flag = isPalindrome(s);
- if (flag) {
- System.out.println("Palindrome !!");
- } else {
- System.out.println("Not a palindrome !! ");
- }
- }
- public static boolean isPalindrome(String s) {
- int length = s.length();
- // An empty string is considered as palindrome
- if (length == 0) {
- return true;
- }
- int startIndex = 0;
- int endIndex = length - 1;
- return palindromeRecursion(s, startIndex, endIndex);
- }
- public static boolean palindromeRecursion(String str, int s, int e) {
- // if there is only character
- if (s == e) {
- return true;
- }
- // if first and last character do not match
- if (str.charAt(s) != str.charAt(e)) {
- return false;
- }
- // If there are more than two characters, check if middle substring is
- // also palindrome or not.
- if (s < e + 1) {
- return palindromeRecursion(str, s + 1, e - 1);
- }
- return true;
- }
- }
Monday, April 23, 2018
Check palindrome using recursion
Subscribe to:
Post Comments (Atom)
How to run standalone mock server on local laptop
Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation Feel fre...
-
Cryptography or cryptology (from Ancient Greek: kryptós "hidden, secret"; graphein, "to write") is the practice and stu...
-
Problem statement: In a dark room,there is a box of 18 white and 5 black gloves. You are allowed to pick one and then you are allowed to k...
-
Please download the standalone wiremock server from Direct download section at the bottom of the page. Download and installation Feel fre...
No comments:
Post a Comment