Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
Example 1:
Input: "A man, a plan, a canal: Panama" Output: true
Example 2:
Input: "race a car" Output: false
문제 풀이:
난이도 쉬움
class Solution {
public boolean isPalindrome(String s) {
s = s.toLowerCase();
int n = s.length();
int left = 0, right = n-1;
while(left < right){
if(!isValid(s.charAt(left))){
left++;
}else if(!isValid(s.charAt(right))){
right--;
}else if(s.charAt(left)!=s.charAt(right)){
return false;
}else {
left++;
right--;
}
}
return true;
}
private boolean isValid(char c){
if((c >= 'a' && c <= 'z') || (c>='0' && c<='9')){
return true;
}
return false;
}
}
'알고리즘 문제풀이 > leetcode' 카테고리의 다른 글
[leetcode 108] Convert Sorted Array to Binary Search Tree (0) | 2020.11.20 |
---|---|
[leetcode 122] Best Time to Buy and Sell Stock II (0) | 2020.11.20 |
[leetcode 118] Pascal's Triangle (0) | 2020.11.19 |
[leetcode 66] Plus One (0) | 2020.11.19 |
[leetcode 88] Merge Sorted Array (0) | 2020.11.19 |
댓글