-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode125_2.java
44 lines (37 loc) · 1.28 KB
/
leetcode125_2.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
class Solution {
public boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
char c1 = s.charAt(left);
char c2 = s.charAt(right);
// check c1 is alphabets and change to lower case
if (Character.isLetter(c1)) {
c1 = Character.toLowerCase(c1);
}
// c1 is not alphabets nor digit
else if (!Character.isDigit(c1)) {
left++;
continue;
}
// check c2 is alphabets and change to lower case
if (Character.isLetter(c2)) {
c2 = Character.toLowerCase(c2);
}
// c2 is not alphabets nor digit
else if (!Character.isDigit(c2)) {
right--;
continue;
}
if (c1 != c2) {
return false;
}
left++;
right--;
}
return true;
}
}
// Runtime: 7 ms, faster than 31.42% of Java online submissions for Valid Palindrome.
// Memory Usage: 40.1 MB, less than 19.47% of Java online submissions for Valid Palindrome.
// time complexity: O(n)
// space complexity: O(1)