File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
src/main/java/com/thealgorithms/recursion Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .thealgorithms .recursion ;
2+
3+ /*
4+ Check if a string is a palindrome using recursion.
5+ Example: "madam" → true, "hello" → false
6+ */
7+ public final class Palindrome {
8+ private Palindrome () {
9+ throw new UnsupportedOperationException ("Utility class" );
10+ }
11+
12+ public static boolean isPalindrome (String str ) {
13+ if (str == null ) {
14+ throw new IllegalArgumentException ("Null string not allowed" );
15+ }
16+ return isPalindromeHelper (str , 0 , str .length () - 1 );
17+ }
18+
19+ private static boolean isPalindromeHelper (String str , int left , int right ) {
20+ if (left >= right ) {
21+ return true ;
22+ }
23+ if (str .charAt (left ) != str .charAt (right )) {
24+ return false ;
25+ }
26+ return isPalindromeHelper (str , left + 1 , right - 1 );
27+ }
28+ }
You can’t perform that action at this time.
0 commit comments