{"id":7585,"date":"2020-10-31T13:42:07","date_gmt":"2020-10-31T20:42:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7585"},"modified":"2020-10-31T14:13:33","modified_gmt":"2020-10-31T21:13:33","slug":"leetcode-1638-count-substrings-that-differ-by-one-character","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1638-count-substrings-that-differ-by-one-character\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1638. Count Substrings That Differ by One Character"},"content":{"rendered":"\n<p>Given two strings&nbsp;<code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>, find the number of ways you can choose a non-empty substring of&nbsp;<code>s<\/code>&nbsp;and replace a&nbsp;<strong>single character<\/strong>&nbsp;by a different character such that the resulting substring is a substring of&nbsp;<code>t<\/code>. In other words, find the number of substrings in&nbsp;<code>s<\/code>&nbsp;that differ from some substring in&nbsp;<code>t<\/code>&nbsp;by&nbsp;<strong>exactly<\/strong>&nbsp;one character.<\/p>\n\n\n\n<p>For example, the underlined substrings in&nbsp;<code>\"computer\"<\/code>&nbsp;and&nbsp;<code>\"computation\"<\/code>&nbsp;only differ by the&nbsp;<code>'e'<\/code>\/<code>'a'<\/code>, so this is a valid way.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of substrings that satisfy the condition above.<\/em><\/p>\n\n\n\n<p>A&nbsp;<strong>substring<\/strong>&nbsp;is a contiguous sequence of characters within a string.<\/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 = \"aba\", t = \"baba\"\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>The following are the pairs of substrings from s and t that differ by exactly 1 character:\n(\"aba\", \"baba\")\n(\"aba\", \"baba\")\n(\"aba\", \"baba\")\n(\"aba\", \"baba\")\n(\"aba\", \"baba\")\n(\"aba\", \"baba\")\nThe underlined portions are the substrings that are chosen from s and t.\n<\/pre>\n\n\n\n<p>\u200b\u200b<strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"ab\", t = \"bb\"\n<strong>Output:<\/strong> 3\n<strong>Explanation: <\/strong>The following are the pairs of substrings from s and t that differ by 1 character:\n(\"ab\", \"bb\")\n(\"ab\", \"bb\")\n(\"ab\", \"bb\")\n\u200b\u200b\u200b\u200bThe underlined portions are the substrings that are chosen from s and t.\n<\/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> s = \"a\", t = \"a\"\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abe\", t = \"bbc\"\n<strong>Output:<\/strong> 10\n<\/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, t.length &lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;and&nbsp;<code>t<\/code>&nbsp;consist of lowercase English letters only.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1: All Pairs + Prefix Matching<\/strong><\/h2>\n\n\n\n<p>match s[i:i+p] with t[j:j+p], is there is one missed char, increase the ans, until there are two miss matched chars or reach the end.<\/p>\n\n\n\n<p>Time complexity: O(m*n*min(m,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  int countSubstrings(string s, string t) {\n    const int m = s.length();\n    const int n = t.length();\n    int ans = 0, diff = 0;\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j, diff = 0)\n        for (int p = 0; i + p < m &#038;&#038; j + p < n &#038;&#038; diff <= 1; ++p)          \n          if ((diff += (s[i + p] != t[j + p])) == 1) ++ans;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Continuous Matching<\/strong><\/h2>\n\n\n\n<p>Start matching s[0] with t[j] and s[i] with t[0]<\/p>\n\n\n\n<p>Time complexity: O(mn)<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  int countSubstrings(string s, string t) {\n    const int m = s.length();\n    const int n = t.length();\n    int ans = 0;\n    auto helper = [&](int i, int j) {      \n      for (int cur = 0, pre = 0; i < m &#038;&#038; j < n; ++i, ++j) {\n        ++cur;\n        if (s[i] != t[j]) {\n          pre = cur;\n          cur = 0;\n        }\n        ans += pre;\n      }\n    };\n    for (int i = 0; i < m; ++i) helper(i, 0);\n    for (int j = 1; j < n; ++j) helper(0, j);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two strings&nbsp;s&nbsp;and&nbsp;t, find the number of ways you can choose a non-empty substring of&nbsp;s&nbsp;and replace a&nbsp;single character&nbsp;by a different character such that the resulting&#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":[177,97,4],"class_list":["post-7585","post","type-post","status-publish","format-standard","hentry","category-string","tag-medium","tag-prefix","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7585","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=7585"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7585\/revisions"}],"predecessor-version":[{"id":7588,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7585\/revisions\/7588"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}