File tree
Expand file treeCollapse file tree4 files changed
+24
-2
lines changed Expand file treeCollapse file tree4 files changed
+24
-2
lines changed Original file line number | Diff line number | Diff line change |
---|
@@ -80,6 +80,11 @@ public static void main(String[] args) {
|
80 | 80 | System.out.println(palindromeService.isPalindrone(inputToCheckForPalindrone));
|
81 | 81 | System.out.println();
|
82 | 82 |
|
| 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 | + |
83 | 88 | }
|
84 | 89 |
|
85 | 90 | }
|
Original file line number | Diff line number | Diff line change |
---|
|
1 | 1 | package com.umer.palindrome;
|
2 | 2 |
|
3 | 3 | public class PalindromeService {
|
4 |
| -
|
| 4 | + |
5 | 5 | 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 | +} |
7 | 24 | }
|
8 | 25 |
|
9 | 26 | }
|
You can’t perform that action at this time.
0 commit comments