Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions valid-palindrome/ymir0804.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public boolean isPalindrome(String s) {
String cleaned = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
if (cleaned.length() == 1 || cleaned.length() == 0) {
return true;
} else if(cleaned.length() ==2) {
return cleaned.charAt(0) == cleaned.charAt(1);
}
int length = cleaned.length();
int mid = length / 2;

String leftPart;
String rightPart;

if (length % 2 == 1) {
leftPart = cleaned.substring(0, mid);
rightPart = cleaned.substring(mid + 1, length);
} else {
leftPart = cleaned.substring(0, mid);
rightPart = cleaned.substring(mid, length);
}

String reversedRightPart = new StringBuilder(rightPart).reverse().toString();

return leftPart.equals(reversedRightPart);

}
}