- 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...
-
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...
-
Problem statement: There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neig...
-
In computer science, ahead-of-time (AOT) compilation is the act of compiling a higher-level programming language such as C or C++, or an...
No comments:
Post a Comment