We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5cb9b97 commit f041548Copy full SHA for f041548
1 file changed
valid-palindrome/ymir0804.java
@@ -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
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