{"id":5031,"date":"2019-04-10T22:16:45","date_gmt":"2019-04-11T05:16:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5031"},"modified":"2019-04-10T22:16:53","modified_gmt":"2019-04-11T05:16:53","slug":"leetcode-125-valid-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-125-valid-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 125. Valid Palindrome"},"content":{"rendered":"\n<p>Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;For the purpose of this problem, we define empty string as valid palindrome.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> \"A man, a plan, a canal: Panama\"\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted; crayon:false\"><strong>Input:<\/strong> \"race a car\"\n<strong>Output:<\/strong> false<\/pre>\n\n\n\n<p>Solution: Two pointers<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  bool isPalindrome(string s) {\n    int i = 0;\n    int j = s.length() - 1;\n    while (i < j) {\n      while (i < j &#038;&#038; !isalnum(s[i])) ++i;\n      while (i < j &#038;&#038; !isalnum(s[j])) --j;\n      if (tolower(s[i++]) != tolower(s[j--])) \n        return false;\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note:&nbsp;For the purpose of this problem, we define empty&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[176],"tags":[222,4,175],"class_list":["post-5031","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-easy","tag-string","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5031"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5031\/revisions"}],"predecessor-version":[{"id":5033,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5031\/revisions\/5033"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}