{"id":8406,"date":"2021-05-01T15:15:00","date_gmt":"2021-05-01T22:15:00","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8406"},"modified":"2021-05-01T15:26:23","modified_gmt":"2021-05-01T22:26:23","slug":"leetcode-1844-replace-all-digits-with-characters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1844-replace-all-digits-with-characters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1844. Replace All Digits with Characters"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>s<\/code>&nbsp;that has lowercase English letters in its&nbsp;<strong>even<\/strong>&nbsp;indices and digits in its&nbsp;<strong>odd<\/strong>&nbsp;indices.<\/p>\n\n\n\n<p>There is a function&nbsp;<code>shift(c, x)<\/code>, where&nbsp;<code>c<\/code>&nbsp;is a character and&nbsp;<code>x<\/code>&nbsp;is a digit, that returns the&nbsp;<code>x<sup>th<\/sup><\/code>&nbsp;character after&nbsp;<code>c<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>shift('a', 5) = 'f'<\/code>&nbsp;and&nbsp;<code>shift('x', 0) = 'x'<\/code>.<\/li><\/ul>\n\n\n\n<p>For every&nbsp;<strong>odd<\/strong>&nbsp;index&nbsp;<code>i<\/code>, you want to replace the digit&nbsp;<code>s[i]<\/code>&nbsp;with&nbsp;<code>shift(s[i-1], s[i])<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>s<\/code><em>&nbsp;after replacing all digits. It is&nbsp;<strong>guaranteed<\/strong>&nbsp;that&nbsp;<\/em><code>shift(s[i-1], s[i])<\/code><em>&nbsp;will never exceed&nbsp;<\/em><code>'z'<\/code>.<\/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> s = \"a1c1e1\"\n<strong>Output:<\/strong> \"abcdef\"\n<strong>Explanation: <\/strong>The digits are replaced as follows:\n- s[1] -&gt; shift('a',1) = 'b'\n- s[3] -&gt; shift('c',1) = 'd'\n- s[5] -&gt; shift('e',1) = 'f'<\/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> s = \"a1b2c3d4e\"\n<strong>Output:<\/strong> \"abbdcfdhe\"\n<strong>Explanation: <\/strong>The digits are replaced as follows:\n- s[1] -&gt; shift('a',1) = 'b'\n- s[3] -&gt; shift('b',2) = 'd'\n- s[5] -&gt; shift('c',3) = 'f'\n- s[7] -&gt; shift('d',4) = 'h'<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;consists only of lowercase English letters and digits.<\/li><li><code>shift(s[i-1], s[i]) &lt;= 'z'<\/code>&nbsp;for all&nbsp;<strong>odd<\/strong>&nbsp;indices&nbsp;<code>i<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string replaceDigits(string s) {\n    for (size_t i = 1; i < s.length(); i += 2)\n      s[i] += s[i - 1] - '0';\n    return s;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;string&nbsp;s&nbsp;that has lowercase English letters in its&nbsp;even&nbsp;indices and digits in its&nbsp;odd&nbsp;indices. There is a function&nbsp;shift(c, x), where&nbsp;c&nbsp;is a character and&nbsp;x&nbsp;is a digit,&#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],"tags":[222,4],"class_list":["post-8406","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8406","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=8406"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8406\/revisions"}],"predecessor-version":[{"id":8410,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8406\/revisions\/8410"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}