返回介绍

solution / 2100-2199 / 2168.Unique Substrings With Equal Digit Frequency / README_EN

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

2168. Unique Substrings With Equal Digit Frequency

中文文档

Description

Given a digit string s, return _the number of unique substrings of _s_ where every digit appears the same number of times._

 

Example 1:

Input: s = "1212"
Output: 5
Explanation: The substrings that meet the requirements are "1", "2", "12", "21", "1212".
Note that although the substring "12" appears twice, it is only counted once.

Example 2:

Input: s = "12321"
Output: 9
Explanation: The substrings that meet the requirements are "1", "2", "3", "12", "23", "32", "21", "123", "321".

 

Constraints:

  • 1 <= s.length <= 1000
  • s consists of digits.

Solutions

Solution 1

class Solution:
  def equalDigitFrequency(self, s: str) -> int:
    def check(i, j):
      v = set()
      for k in range(10):
        cnt = presum[j + 1][k] - presum[i][k]
        if cnt > 0:
          v.add(cnt)
        if len(v) > 1:
          return False
      return True

    n = len(s)
    presum = [[0] * 10 for _ in range(n + 1)]
    for i, c in enumerate(s):
      presum[i + 1][int(c)] += 1
      for j in range(10):
        presum[i + 1][j] += presum[i][j]
    vis = set(s[i : j + 1] for i in range(n) for j in range(i, n) if check(i, j))
    return len(vis)
class Solution {
  public int equalDigitFrequency(String s) {
    int n = s.length();
    int[][] presum = new int[n + 1][10];
    for (int i = 0; i < n; ++i) {
      ++presum[i + 1][s.charAt(i) - '0'];
      for (int j = 0; j < 10; ++j) {
        presum[i + 1][j] += presum[i][j];
      }
    }
    Set<String> vis = new HashSet<>();
    for (int i = 0; i < n; ++i) {
      for (int j = i; j < n; ++j) {
        if (check(i, j, presum)) {
          vis.add(s.substring(i, j + 1));
        }
      }
    }
    return vis.size();
  }

  private boolean check(int i, int j, int[][] presum) {
    Set<Integer> v = new HashSet<>();
    for (int k = 0; k < 10; ++k) {
      int cnt = presum[j + 1][k] - presum[i][k];
      if (cnt > 0) {
        v.add(cnt);
      }
      if (v.size() > 1) {
        return false;
      }
    }
    return true;
  }
}
func equalDigitFrequency(s string) int {
  n := len(s)
  presum := make([][]int, n+1)
  for i := range presum {
    presum[i] = make([]int, 10)
  }
  for i, c := range s {
    presum[i+1][c-'0']++
    for j := 0; j < 10; j++ {
      presum[i+1][j] += presum[i][j]
    }
  }
  check := func(i, j int) bool {
    v := make(map[int]bool)
    for k := 0; k < 10; k++ {
      cnt := presum[j+1][k] - presum[i][k]
      if cnt > 0 {
        v[cnt] = true
      }
      if len(v) > 1 {
        return false
      }
    }
    return true
  }
  vis := make(map[string]bool)
  for i := 0; i < n; i++ {
    for j := i; j < n; j++ {
      if check(i, j) {
        vis[s[i:j+1]] = true
      }
    }
  }
  return len(vis)
}

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

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

发布评论

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