125. Valid Palindrome

网友投稿 734 2022-09-04

125. Valid Palindrome

125. 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.

Note: Have you consider that the string might be empty? This is a good question to ask during an interview.

For the purpose of this problem, we define empty string as valid palindrome.

public class Solution { public boolean isPalindrome(String s) { int i = 0; int j = s.length() - 1; while (i < j) { while ( ! isLetterOrNumber(s.charAt(i)) && i < j) i ++; while ( ! isLetterOrNumber(s.charAt(j)) && i < j) j --; if(i < j && Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(j))) return false; i ++; j --; } return true; } public static boolean isLetterOrNumber(char i) { if(i >= '0' && i <= '9' || i >= 'a' && i <= 'z' || i >= 'A' && i <= 'Z') return true; return false; }}

/** * @param {string} s * @return {boolean} */var isPalindrome = function(s) { s = s.replace(/\W/g, '').toLowerCase(); let left = 0; let right = s.length - 1; while (left < right) { if (s[left] !== s[right]) return false; left++; right--; } return true;};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:121. Best Time to Buy and Sell Stock
下一篇:精心整理了一套MySQL 常用命令(请整理一下)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~