{"id":10077,"date":"2023-08-23T21:38:17","date_gmt":"2023-08-24T04:38:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10077"},"modified":"2023-08-23T21:44:48","modified_gmt":"2023-08-24T04:44:48","slug":"leetcode-2825-make-string-a-subsequence-using-cyclic-increments","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-2825-make-string-a-subsequence-using-cyclic-increments\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2825. Make String a Subsequence Using Cyclic Increments"},"content":{"rendered":"\n<p>You are given two&nbsp;<strong>0-indexed<\/strong>&nbsp;strings&nbsp;<code>str1<\/code>&nbsp;and&nbsp;<code>str2<\/code>.<\/p>\n\n\n\n<p>In an operation, you select a&nbsp;<strong>set<\/strong>&nbsp;of indices in&nbsp;<code>str1<\/code>, and for each index&nbsp;<code>i<\/code>&nbsp;in the set, increment&nbsp;<code>str1[i]<\/code>&nbsp;to the next character&nbsp;<strong>cyclically<\/strong>. That is&nbsp;<code>'a'<\/code>&nbsp;becomes&nbsp;<code>'b'<\/code>,&nbsp;<code>'b'<\/code>&nbsp;becomes&nbsp;<code>'c'<\/code>, and so on, and&nbsp;<code>'z'<\/code>&nbsp;becomes&nbsp;<code>'a'<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if it is possible to make&nbsp;<\/em><code>str2<\/code>&nbsp;<em>a subsequence of&nbsp;<\/em><code>str1<\/code>&nbsp;<em>by performing the operation&nbsp;<strong>at most once<\/strong><\/em>,&nbsp;<em>and<\/em>&nbsp;<code>false<\/code>&nbsp;<em>otherwise<\/em>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.<\/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> str1 = \"abc\", str2 = \"ad\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Select index 2 in str1.\nIncrement str1[2] to become 'd'. \nHence, str1 becomes \"abd\" and str2 is now a subsequence. Therefore, true is returned.<\/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> str1 = \"zc\", str2 = \"ad\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Select indices 0 and 1 in str1. \nIncrement str1[0] to become 'a'. \nIncrement str1[1] to become 'd'. \nHence, str1 becomes \"ad\" and str2 is now a subsequence. Therefore, true is returned.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> str1 = \"ab\", str2 = \"d\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once. \nTherefore, false is returned.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= str1.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= str2.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>str1<\/code>&nbsp;and&nbsp;<code>str2<\/code>&nbsp;consist of only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Two pointers<\/strong><\/h2>\n\n\n\n<p>s1[i] and s2[j] can match if <br>s1[i] == s2[j] or inc(s1[i]) == s2[j]<\/p>\n\n\n\n<p>If matched: ++i; ++j else ++i.<\/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 canMakeSubsequence(string_view s1, string_view s2) {    \n    for (int i = 0, j = 0, n = s1.size(), m = s2.size(); i < n; ++i)\n      if (s1[i] == s2[j] || (s1[i] - 'a' + 1) % 26 == s2[j] - 'a')\n        if (++j == m) return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Iterator version<\/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 canMakeSubsequence(string_view s1, string_view s2) {    \n    for (auto i = begin(s1), j = begin(s2); i != end(s1); ++i)\n      if (*i == *j || (*i - 'a' + 1) % 26 == *j - 'a')\n        if (++j == end(s2)) return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two&nbsp;0-indexed&nbsp;strings&nbsp;str1&nbsp;and&nbsp;str2. In an operation, you select a&nbsp;set&nbsp;of indices in&nbsp;str1, and for each index&nbsp;i&nbsp;in the set, increment&nbsp;str1[i]&nbsp;to the next character&nbsp;cyclically. That is&nbsp;&#8216;a&#8217;&nbsp;becomes&nbsp;&#8216;b&#8217;,&nbsp;&#8216;b&#8217;&nbsp;becomes&nbsp;&#8216;c&#8217;, and&#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":[177,4,229,175],"class_list":["post-10077","post","type-post","status-publish","format-standard","hentry","category-two-pointers","tag-medium","tag-string","tag-subsequence","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10077","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=10077"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10077\/revisions"}],"predecessor-version":[{"id":10080,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10077\/revisions\/10080"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}