返回介绍

solution / 2200-2299 / 2272.Substring With Largest Variance / README_EN

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

2272. Substring With Largest Variance

中文文档

Description

The variance of a string is defined as the largest difference between the number of occurrences of any 2 characters present in the string. Note the two characters may or may not be the same.

Given a string s consisting of lowercase English letters only, return _the largest variance possible among all substrings of_ s.

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

 

Example 1:

Input: s = "aababbb"
Output: 3
Explanation:
All possible variances along with their respective substrings are listed below:
- Variance 0 for substrings "a", "aa", "ab", "abab", "aababb", "ba", "b", "bb", and "bbb".
- Variance 1 for substrings "aab", "aba", "abb", "aabab", "ababb", "aababbb", and "bab".
- Variance 2 for substrings "aaba", "ababbb", "abbb", and "babb".
- Variance 3 for substring "babbb".
Since the largest possible variance is 3, we return it.

Example 2:

Input: s = "abcde"
Output: 0
Explanation:
No letter occurs more than once in s, so the variance of every substring is 0.

 

Constraints:

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

Solutions

Solution 1

class Solution:
  def largestVariance(self, s: str) -> int:
    ans = 0
    for a, b in permutations(ascii_lowercase, 2):
      if a == b:
        continue
      f = [0, -inf]
      for c in s:
        if c == a:
          f[0], f[1] = f[0] + 1, f[1] + 1
        elif c == b:
          f[1] = max(f[1] - 1, f[0] - 1)
          f[0] = 0
        if ans < f[1]:
          ans = f[1]
    return ans
class Solution {
  public int largestVariance(String s) {
    int n = s.length();
    int ans = 0;
    for (char a = 'a'; a <= 'z'; ++a) {
      for (char b = 'a'; b <= 'z'; ++b) {
        if (a == b) {
          continue;
        }
        int[] f = new int[] {0, -n};
        for (int i = 0; i < n; ++i) {
          if (s.charAt(i) == a) {
            f[0]++;
            f[1]++;
          } else if (s.charAt(i) == b) {
            f[1] = Math.max(f[0] - 1, f[1] - 1);
            f[0] = 0;
          }
          ans = Math.max(ans, f[1]);
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int largestVariance(string s) {
    int n = s.size();
    int ans = 0;
    for (char a = 'a'; a <= 'z'; ++a) {
      for (char b = 'a'; b <= 'z'; ++b) {
        if (a == b) continue;
        int f[2] = {0, -n};
        for (char c : s) {
          if (c == a) {
            f[0]++;
            f[1]++;
          } else if (c == b) {
            f[1] = max(f[1] - 1, f[0] - 1);
            f[0] = 0;
          }
          ans = max(ans, f[1]);
        }
      }
    }
    return ans;
  }
};
func largestVariance(s string) int {
  ans, n := 0, len(s)
  for a := 'a'; a <= 'z'; a++ {
    for b := 'a'; b <= 'z'; b++ {
      if a == b {
        continue
      }
      f := [2]int{0, -n}
      for _, c := range s {
        if c == a {
          f[0]++
          f[1]++
        } else if c == b {
          f[1] = max(f[1]-1, f[0]-1)
          f[0] = 0
        }
        ans = max(ans, f[1])
      }
    }
  }
  return ans
}

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

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

发布评论

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