반응형
Leetcode - Valid Palindrome
설명
Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example:
Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.
제한사항
- s consists only of printable ASCII characters.
소스코드
class Solution {
public boolean isPalindrome(String s) {
boolean answer = true;
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
char[] chars = s.toCharArray();
for (int i=0; i<chars.length; i++) {
if (chars[i] != chars[chars.length - 1 - i]) {
return false;
}
}
return answer;
}
}
팰린드롬 문제2!
반응형