-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
26 lines (22 loc) · 788 Bytes
/
Solution.java
File metadata and controls
26 lines (22 loc) · 788 Bytes
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
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String A = sc.next();
System.out.println((isPalindromeRecursive(A) ? "Yes" : "No"));
System.out.println((isPalindrome(A) ? "Yes" : "No"));
}
private static boolean isPalindromeRecursive(String s) {
if (s.length() <= 1) return true;
if (s.charAt(0) == s.charAt(s.length() - 1)) return isPalindromeRecursive(s.substring(1, s.length() - 1));
return false;
}
private static boolean isPalindrome(String s) {
int i = 0;
int j = s.length() - 1;
while (i++ <= j--) {
if (s.charAt(i) != s.charAt(j)) return false;
}
return true;
}
}