Skip to content

Commit f041548

Browse files
committed
Valid Palindrome 문제 풀이
1 parent 5cb9b97 commit f041548

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

valid-palindrome/ymir0804.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
String cleaned = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
4+
if (cleaned.length() == 1 || cleaned.length() == 0) {
5+
return true;
6+
} else if(cleaned.length() ==2) {
7+
return cleaned.charAt(0) == cleaned.charAt(1);
8+
}
9+
int length = cleaned.length();
10+
int mid = length / 2;
11+
12+
String leftPart;
13+
String rightPart;
14+
15+
if (length % 2 == 1) {
16+
leftPart = cleaned.substring(0, mid);
17+
rightPart = cleaned.substring(mid + 1, length);
18+
} else {
19+
leftPart = cleaned.substring(0, mid);
20+
rightPart = cleaned.substring(mid, length);
21+
}
22+
23+
String reversedRightPart = new StringBuilder(rightPart).reverse().toString();
24+
25+
return leftPart.equals(reversedRightPart);
26+
27+
}
28+
}

0 commit comments

Comments
 (0)