Skip to content

Commit 141b530

Browse files
authored
Create 125. Valid Palindrome.java
1 parent 02f591b commit 141b530

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// 투포인터
2+
// O(n)
3+
4+
class Solution {
5+
public boolean isPalindrome(String s) {
6+
StringBuilder sb = new StringBuilder();
7+
for (int i = 0; i<s.length(); i++) {
8+
char c = Character.toLowerCase(s.charAt(i));
9+
if (isLetter(c) || isNumber(c)) sb.append(c);
10+
}
11+
12+
int left = 0;
13+
int right = sb.length() -1;
14+
while (left<right) {
15+
if (sb.charAt(left) != sb.charAt(right)) return false;
16+
left++;
17+
right--;
18+
}
19+
return true;
20+
}
21+
22+
boolean isLetter(int ord) {
23+
return ord>=97 && ord <=122;
24+
}
25+
26+
boolean isNumber(int ord) {
27+
return ord>=48 && ord <=57;
28+
}
29+
}

0 commit comments

Comments
 (0)