-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path125. Valid Palindrome.java
51 lines (40 loc) · 1.17 KB
/
125. Valid Palindrome.java
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
/*
Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example 1:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
Example 2:
Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.
Constraints:
1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.*/
//Solution
class Solution {
public boolean isPalindrome(String str) {
str = str.toLowerCase();
StringBuilder x = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')){
x.append(ch);
}
}
return palin(x);
}
boolean palin(StringBuilder str){
if(str.length() == 1){
return true;
}
for (int i = 0; i < str.length() / 2; i++) {
char start = str.charAt(i);
char end = str.charAt(str.length() - 1 - i);
if(start != end){
return false;
}
}
return true;
}
}