Skip to content

Commit f71ccbf

Browse files
committed
Valid Palindrome solution
1 parent 2e7b005 commit f71ccbf

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* μ‹œκ°„λ³΅μž‘λ„ : O(n)
3+
* κ³΅κ°„λ³΅μž‘λ„ : O(n)
4+
*/
5+
public class kimjunyoung90 {
6+
public boolean isPalindrome(String s) {
7+
//1. λŒ€λ¬Έμžλ₯Ό μ†Œλ¬Έμžλ‘œ λ³€ν™˜
8+
s = s.toLowerCase();
9+
10+
//2. μ˜μ–΄ 숫자 μ™Έ 문자 제거
11+
s = s.replaceAll("[^a-z0-9]", "");
12+
13+
//3. μ•žμ—μ„œ μ½λ‚˜ λ’€μ—μ„œ μ½λ‚˜ λ™μΌν•œμ§€ 확인(pointer μ‚¬μš©)
14+
int left = 0, right = s.length() - 1;
15+
while(left < right) {
16+
char leftChar = s.charAt(left);
17+
char rightChar = s.charAt(right);
18+
if(leftChar != rightChar) return false;
19+
left++;
20+
right--;
21+
}
22+
return true;
23+
}
24+
}

0 commit comments

Comments
Β (0)