返回介绍

solution / 1600-1699 / 1638.Count Substrings That Differ by One Character / README_EN

发布于 2024-06-17 01:03:16 字数 7851 浏览 0 评论 0 收藏 0

1638. Count Substrings That Differ by One Character

中文文档

Description

Given two strings s and t, find the number of ways you can choose a non-empty substring of s and replace a single character by a different character such that the resulting substring is a substring of t. In other words, find the number of substrings in s that differ from some substring in t by exactly one character.

For example, the underlined substrings in "computer" and "computation" only differ by the 'e'/'a', so this is a valid way.

Return _the number of substrings that satisfy the condition above._

A substring is a contiguous sequence of characters within a string.

 

Example 1:

Input: s = "aba", t = "baba"
Output: 6
Explanation: The following are the pairs of substrings from s and t that differ by exactly 1 character:
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
("aba", "baba")
The underlined portions are the substrings that are chosen from s and t.

​​Example 2:

Input: s = "ab", t = "bb"
Output: 3
Explanation: The following are the pairs of substrings from s and t that differ by 1 character:
("ab", "bb")
("ab", "bb")
("ab", "bb")
​​​​The underlined portions are the substrings that are chosen from s and t.

 

Constraints:

  • 1 <= s.length, t.length <= 100
  • s and t consist of lowercase English letters only.

Solutions

Solution 1

class Solution:
  def countSubstrings(self, s: str, t: str) -> int:
    ans = 0
    m, n = len(s), len(t)
    for i, a in enumerate(s):
      for j, b in enumerate(t):
        if a != b:
          l = r = 0
          while i > l and j > l and s[i - l - 1] == t[j - l - 1]:
            l += 1
          while (
            i + r + 1 < m and j + r + 1 < n and s[i + r + 1] == t[j + r + 1]
          ):
            r += 1
          ans += (l + 1) * (r + 1)
    return ans
class Solution {
  public int countSubstrings(String s, String t) {
    int ans = 0;
    int m = s.length(), n = t.length();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (s.charAt(i) != t.charAt(j)) {
          int l = 0, r = 0;
          while (i - l > 0 && j - l > 0 && s.charAt(i - l - 1) == t.charAt(j - l - 1)) {
            ++l;
          }
          while (i + r + 1 < m && j + r + 1 < n
            && s.charAt(i + r + 1) == t.charAt(j + r + 1)) {
            ++r;
          }
          ans += (l + 1) * (r + 1);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countSubstrings(string s, string t) {
    int ans = 0;
    int m = s.size(), n = t.size();
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (s[i] != t[j]) {
          int l = 0, r = 0;
          while (i - l > 0 && j - l > 0 && s[i - l - 1] == t[j - l - 1]) {
            ++l;
          }
          while (i + r + 1 < m && j + r + 1 < n && s[i + r + 1] == t[j + r + 1]) {
            ++r;
          }
          ans += (l + 1) * (r + 1);
        }
      }
    }
    return ans;
  }
};
func countSubstrings(s string, t string) (ans int) {
  m, n := len(s), len(t)
  for i, a := range s {
    for j, b := range t {
      if a != b {
        l, r := 0, 0
        for i > l && j > l && s[i-l-1] == t[j-l-1] {
          l++
        }
        for i+r+1 < m && j+r+1 < n && s[i+r+1] == t[j+r+1] {
          r++
        }
        ans += (l + 1) * (r + 1)
      }
    }
  }
  return
}

Solution 2

class Solution:
  def countSubstrings(self, s: str, t: str) -> int:
    ans = 0
    m, n = len(s), len(t)
    f = [[0] * (n + 1) for _ in range(m + 1)]
    g = [[0] * (n + 1) for _ in range(m + 1)]
    for i, a in enumerate(s, 1):
      for j, b in enumerate(t, 1):
        if a == b:
          f[i][j] = f[i - 1][j - 1] + 1
    for i in range(m - 1, -1, -1):
      for j in range(n - 1, -1, -1):
        if s[i] == t[j]:
          g[i][j] = g[i + 1][j + 1] + 1
        else:
          ans += (f[i][j] + 1) * (g[i + 1][j + 1] + 1)
    return ans
class Solution {
  public int countSubstrings(String s, String t) {
    int ans = 0;
    int m = s.length(), n = t.length();
    int[][] f = new int[m + 1][n + 1];
    int[][] g = new int[m + 1][n + 1];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (s.charAt(i) == t.charAt(j)) {
          f[i + 1][j + 1] = f[i][j] + 1;
        }
      }
    }
    for (int i = m - 1; i >= 0; --i) {
      for (int j = n - 1; j >= 0; --j) {
        if (s.charAt(i) == t.charAt(j)) {
          g[i][j] = g[i + 1][j + 1] + 1;
        } else {
          ans += (f[i][j] + 1) * (g[i + 1][j + 1] + 1);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countSubstrings(string s, string t) {
    int ans = 0;
    int m = s.length(), n = t.length();
    int f[m + 1][n + 1];
    int g[m + 1][n + 1];
    memset(f, 0, sizeof(f));
    memset(g, 0, sizeof(g));
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (s[i] == t[j]) {
          f[i + 1][j + 1] = f[i][j] + 1;
        }
      }
    }
    for (int i = m - 1; i >= 0; --i) {
      for (int j = n - 1; j >= 0; --j) {
        if (s[i] == t[j]) {
          g[i][j] = g[i + 1][j + 1] + 1;
        } else {
          ans += (f[i][j] + 1) * (g[i + 1][j + 1] + 1);
        }
      }
    }
    return ans;
  }
};
func countSubstrings(s string, t string) (ans int) {
  m, n := len(s), len(t)
  f := make([][]int, m+1)
  g := make([][]int, m+1)
  for i := range f {
    f[i] = make([]int, n+1)
    g[i] = make([]int, n+1)
  }
  for i, a := range s {
    for j, b := range t {
      if a == b {
        f[i+1][j+1] = f[i][j] + 1
      }
    }
  }
  for i := m - 1; i >= 0; i-- {
    for j := n - 1; j >= 0; j-- {
      if s[i] == t[j] {
        g[i][j] = g[i+1][j+1] + 1
      } else {
        ans += (f[i][j] + 1) * (g[i+1][j+1] + 1)
      }
    }
  }
  return
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文