-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path125.java
More file actions
48 lines (48 loc) · 1.37 KB
/
125.java
File metadata and controls
48 lines (48 loc) · 1.37 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
// 125. Valid Palindrome
// 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.
//
// Runtime: 3 ms, faster than 69.31% of Java online submissions for Valid Palindrome.
// Memory Usage: 38.8 MB, less than 81.69% of Java online submissions for Valid Palindrome.
class Solution {
public boolean isPalindrome(String s) {
s = s.toLowerCase();
int start = 0;
int end = s.length() - 1;
while (start < end) {
if (!Character.isAlphabetic(s.charAt(start)) &&
!Character.isDigit(s.charAt(start))) {
start++;
continue;
}
if (!Character.isAlphabetic(s.charAt(end))&&
!Character.isDigit(s.charAt(end))) {
end--;
continue;
}
if (s.charAt(start) == s.charAt(end)) {
start++;
end--;
} else {
return false;
}
}
return true;
}
}