Wednesday, March 20, 2013
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
public class Solution {
boolean isAlpha(Character a){
return ('A' <= a && a <= 'Z') ||
('0' <= a && a <= '9');
}
public boolean isPalindrome(String s) {
// Start typing your Java solution below
// DO NOT write main() function
s = s.toUpperCase();
int i = 0;
int j = s.length()-1;
while(i<j){
if(!isAlpha(s.charAt(i))){ i++; continue;}
if(!isAlpha(s.charAt(j))){ j--; continue;}
if(s.charAt(i) == s.charAt(j)){
i++; j--;
}
else return false;
}
return true;
}
}
Subscribe to:
Post Comments (Atom)
Manacher's Longest Palindromic Substring Algorithm
http://manacher-viz.s3-website-us-east-1.amazonaws.com/#/
-
Suppose a bus is running on a loop route. It takes 10 mins for the bus to finish the route. If a guy arrives at a bus stop at uniformly rand...
-
Question: You are playing a card game with me. Suppose I shuffle a deck of 52 cards, then I show you the card one by one to you from top t...
-
Move Objects There are N objects kept in a row. The ith object is at position x_i. You want to partition them into K groups. You want ...
No comments:
Post a Comment