File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public static void main(String[] args) {
8080
System.out.println(palindromeService.isPalindrone(inputToCheckForPalindrone));
8181
System.out.println();
8282

83+
inputToCheckForPalindrone = "foobar";
84+
System.out.println("Checking if the following string is a Palindrome or not: " + inputToCheckForPalindrone);
85+
System.out.println(palindromeService.isPalindrone(inputToCheckForPalindrone));
86+
System.out.println();
87+
8388
}
8489

8590
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
package com.umer.palindrome;
22

33
public class PalindromeService {
4-
4+
55
public boolean isPalindrone(String input) {
6-
return false;
6+
// Exit condition
7+
8+
// 0 means this is a word with even number of alphabets and all pairs have
9+
// already been verified with no alphabets left
10+
11+
// 1 means this is a word with odd number of alphabets and all pairs have
12+
// already been verified with one alphabet left
13+
if (input.length() == 0 || input.length() == 1) {
14+
return true;
15+
}
16+
// recursive flow
17+
if (input.charAt(0) == input.charAt(input.length() - 1)) {
18+
// The alphabets match so continue further with a smaller subset of the string
19+
return isPalindrone(input.substring(1, input.length() - 1));
20+
} else {
21+
// The alphabets do not match. Hence, return false.
22+
return false;
23+
}
724
}
825

926
}

0 commit comments

Comments
 (0)