-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckPalindrome.java
More file actions
43 lines (29 loc) · 988 Bytes
/
Copy pathCheckPalindrome.java
File metadata and controls
43 lines (29 loc) · 988 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package questions.codesignal.checkpalindrome;
public class CheckPalindrome {
public static boolean checkPalindrome(String str) {
if (str.length() == 1) {
return true;
}
int i = 0; // left side index
int j = str.length() - 1; // right side index
while (i < j) {
if (str.charAt(i) != str.charAt(j)) {
return false;
}
i++;
j--;
}
return true;
}
public static void main(String[] args) {
String s1 = "aabaa";
boolean p1 = checkPalindrome(s1);
System.out.println(s1 + " is" + (p1 ? "" : " Not") + " palindrome");
String s2 = "abac";
boolean p2 = checkPalindrome(s2);
System.out.println(s2 + " is" + (p2 ? "" : " Not") + " palindrome");
String s3 = "a";
boolean p3 = checkPalindrome(s3);
System.out.println(s3 + " is" + (p3 ? "" : " Not") + " palindrome");
}
}