Skip to content

Commit 096914a

Browse files
committed
Valid Palindrome
1 parent 0c04f6e commit 096914a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Runtime: 1 ms(Beats: 100.00 %)
3+
Time Complexity: O(n)
4+
5+
Memory: 43.00 MB(Beats: 64.54 %)
6+
Space Complexity: O(1)
7+
... ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ String s๋Š” space complexity ๊ณ„์‚ฐ์—์„œ ์ œ์™ธํ•˜๊ณ , ์ œ๊ฐ€ ์ถ”๊ฐ€ํ•œ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด์„œ๋งŒ ๊ณ„์‚ฐํ•˜๋ฉด ๋ ๊นŒ์š”?
8+
*/
9+
10+
class Solution {
11+
public boolean isPalindrome(String s) {
12+
for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
13+
char a = s.charAt(i);
14+
while (a < '0' || (a > '9' && a < 'A') || (a > 'Z' && a < 'a') || a > 'z') {
15+
a = s.charAt(++i);
16+
if (i >= j) {
17+
return true;
18+
}
19+
}
20+
if (a <= 'Z') {
21+
a += ('a' - 'A');
22+
}
23+
24+
char b = s.charAt(j);
25+
while (b < '0' || (b > '9' && b < 'A') || (b > 'Z' && b < 'a') || b > 'z') {
26+
b = s.charAt(--j);
27+
if (i >= j) {
28+
return true;
29+
}
30+
}
31+
if (b <= 'Z') {
32+
b += ('a' - 'A');
33+
}
34+
35+
if (a != b) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}

0 commit comments

Comments
ย (0)