-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckPalindromeRecursive.java
More file actions
64 lines (55 loc) · 1.56 KB
/
CheckPalindromeRecursive.java
File metadata and controls
64 lines (55 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Check Palindrome (recursive)
// Send Feedback
// Check whether a given String 'S' is a palindrome using recursion.
// Return true or false.
// Note:
// You don’t need to print anything. Just implement the given function.
// Example:
// Input: s = "racecar"
// Output: true
// Explanation: "racecar" is a palindrome.
// Input Format:
// The first and only line of the input contains string S.
// Output format:
// Return a boolean value True or False.
// Sample Input 1:
// abbba
// Sample Output 1:
// true
// Explanation Of Sample Input 1 :
// “abbba” is a palindrome
// Sample Input 2:
// abcd
// Sample Output 2:
// false
// Explanation Of Sample Input 2 :
// “abcd” is not a palindrome.
// Constraints:
// 0 <= |S| <= 10^6
// where |S| represents the length of string S.
import java.util.Scanner;
/**
* CheckPalindromeRecursive
*/
public class CheckPalindromeRecursive {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter string : ");
String input = s.nextLine();
boolean ans = isPalindrome(input);
System.out.print("Output : " + ans);
s.close();
}
public static boolean isPalindrome(String input) {
return isPalindromeHelper(input, 0, input.length() - 1);
}
public static boolean isPalindromeHelper(String input, int si, int ei) {
if (si >= ei) {
return true;
}
if (input.charAt(si) != input.charAt(ei)) {
return false;
}
return isPalindromeHelper(input, si + 1, ei - 1);
}
}