{"id":4157,"date":"2018-10-06T20:43:41","date_gmt":"2018-10-07T03:43:41","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4157"},"modified":"2018-10-06T20:47:09","modified_gmt":"2018-10-07T03:47:09","slug":"leetcode-917-reverse-only-letters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-917-reverse-only-letters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 917. Reverse Only Letters"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a string\u00a0<code>S<\/code>, return the &#8220;reversed&#8221; string where all characters that are not a letter\u00a0stay in the same place, and all letters reverse their positions.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">\"ab-cd\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">\"dc-ba\"<\/span>\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">\"a-bC-dEf-ghIj\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">\"j-Ih-gfE-dCba\"<\/span>\r\n<\/pre>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">\"Test1ng-Leet=code-Q!\"<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">\"Qedo1ct-eeLg=ntse-T!\"<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>S.length &lt;= 100<\/code><\/li>\n<li><code>33 &lt;= S[i].ASCIIcode &lt;= 122<\/code><\/li>\n<li><code>S<\/code>\u00a0doesn&#8217;t contain\u00a0<code>\\<\/code>\u00a0or\u00a0<code>\"<\/code><\/li>\n<\/ol>\n<h1><strong>Solution: Two Pointers<\/strong><\/h1>\n<p>Time complexity: O(n)<\/p>\n<p>Space complexity: O(1) &#8211; in place<\/p>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/index<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  string reverseOnlyLetters(string S) {\r\n    int i = 0;\r\n    int j = S.length() - 1;\r\n    while (i &lt; j) {\r\n      if (isalpha(S[i]) &amp;&amp; isalpha(S[j])) {\r\n        swap(S[i++], S[j--]);        \r\n      } else {\r\n        if (!isalpha(S[i])) ++i;\r\n        if (!isalpha(S[j])) --j;\r\n      }\r\n    }\r\n    return S;\r\n  }\r\n};<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/iter<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n  string reverseOnlyLetters(string S) {\r\n    auto it1 = begin(S);\r\n    auto it2 = prev(end(S));\r\n    while (it1 &lt; it2) {\r\n      if (isalpha(*it1) &amp;&amp; isalpha(*it2)) {\r\n        swap(*it1++, *it2--);\r\n      } else {\r\n        if (!isalpha(*it1)) ++it1;\r\n        if (!isalpha(*it2)) --it2;\r\n      }\r\n    }\r\n    return S;\r\n  }\r\n};<\/pre>\n<\/div><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a string\u00a0S, return the &#8220;reversed&#8221; string where all characters that are not a letter\u00a0stay in the same place, and all letters reverse their&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47,176],"tags":[222,246,4,175],"class_list":["post-4157","post","type-post","status-publish","format-standard","hentry","category-string","category-two-pointers","tag-easy","tag-reverse","tag-string","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4157","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=4157"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4157\/revisions"}],"predecessor-version":[{"id":4161,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4157\/revisions\/4161"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}