返回介绍

solution / 2700-2799 / 2743.Count Substrings Without Repeating Character / README_EN

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

2743. Count Substrings Without Repeating Character

中文文档

Description

You are given a string s consisting only of lowercase English letters. We call a substring special if it contains no character which has occurred at least twice (in other words, it does not contain a repeating character). Your task is to count the number of special substrings. For example, in the string "pop", the substring "po" is a special substring, however, "pop" is not special (since 'p' has occurred twice).

Return _the number of special substrings._

A substring is a contiguous sequence of characters within a string. For example, "abc" is a substring of "abcd", but "acd" is not.

 

Example 1:

Input: s = "abcd"
Output: 10
Explanation: Since each character occurs once, every substring is a special substring. We have 4 substrings of length one, 3 of length two, 2 of length three, and 1 substring of length four. So overall there are 4 + 3 + 2 + 1 = 10 special substrings.

Example 2:

Input: s = "ooo"
Output: 3
Explanation: Any substring with a length of at least two contains a repeating character. So we have to count the number of substrings of length one, which is 3.

Example 3:

Input: s = "abab"
Output: 7
Explanation: Special substrings are as follows (sorted by their start positions):
Special substrings of length 1: "a", "b", "a", "b"
Special substrings of length 2: "ab", "ba", "ab"
And it can be shown that there are no special substrings with a length of at least three. So the answer would be 4 + 3 = 7.

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters

Solutions

Solution 1

class Solution:
  def numberOfSpecialSubstrings(self, s: str) -> int:
    cnt = Counter()
    ans = j = 0
    for i, c in enumerate(s):
      cnt[c] += 1
      while cnt[c] > 1:
        cnt[s[j]] -= 1
        j += 1
      ans += i - j + 1
    return ans
class Solution {
  public int numberOfSpecialSubstrings(String s) {
    int n = s.length();
    int ans = 0;
    int[] cnt = new int[26];
    for (int i = 0, j = 0; i < n; ++i) {
      int k = s.charAt(i) - 'a';
      ++cnt[k];
      while (cnt[k] > 1) {
        --cnt[s.charAt(j++) - 'a'];
      }
      ans += i - j + 1;
    }
    return ans;
  }
}
class Solution {
public:
  int numberOfSpecialSubstrings(string s) {
    int n = s.size();
    int cnt[26]{};
    int ans = 0;
    for (int i = 0, j = 0; i < n; ++i) {
      int k = s[i] - 'a';
      ++cnt[k];
      while (cnt[k] > 1) {
        --cnt[s[j++] - 'a'];
      }
      ans += i - j + 1;
    }
    return ans;
  }
};
func numberOfSpecialSubstrings(s string) (ans int) {
  j := 0
  cnt := [26]int{}
  for i, c := range s {
    k := c - 'a'
    cnt[k]++
    for cnt[k] > 1 {
      cnt[s[j]-'a']--
      j++
    }
    ans += i - j + 1
  }
  return
}
function numberOfSpecialSubstrings(s: string): number {
  const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
  const n = s.length;
  const cnt: number[] = Array(26).fill(0);
  let ans = 0;
  for (let i = 0, j = 0; i < n; ++i) {
    const k = idx(s[i]);
    ++cnt[k];
    while (cnt[k] > 1) {
      --cnt[idx(s[j++])];
    }
    ans += i - j + 1;
  }
  return ans;
}

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

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

发布评论

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