Skip to content

Commit 2dc5562

Browse files
committed
valid palindrome
1 parent 9c8fe48 commit 2dc5562

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Runtime: 2ms
3+
* Time Complexity: O(n)
4+
*
5+
* Memory: 44.29MB
6+
* Space Complexity: O(1)
7+
*
8+
* Approach: ํˆฌ ํฌ์ธํ„ฐ
9+
* 1) ๋ฌธ์ž์—ด์˜ ์–‘ ๋์—์„œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์„ค์ •
10+
* 2) ํฌ์ธํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž๊ฐ€ ์˜์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ, ํ•ด๋‹น ํฌ์ธํ„ฐ๋ฅผ ์ด๋™
11+
*/
12+
class Solution {
13+
public boolean isPalindrome(String s) {
14+
int start = 0;
15+
int end = s.length()-1;
16+
17+
while (start < end) {
18+
char currLeft = s.charAt(start);
19+
char currRight = s.charAt(end);
20+
21+
if (!Character.isLetterOrDigit(currLeft)) {
22+
start++;
23+
} else if (!Character.isLetterOrDigit(currRight)) {
24+
end--;
25+
} else {
26+
if (Character.toLowerCase(currLeft) != Character.toLowerCase(currRight)) {
27+
return false;
28+
}
29+
30+
start++;
31+
end--;
32+
}
33+
}
34+
35+
return true;
36+
}
37+
}

0 commit comments

Comments
ย (0)