当前位置: 首页 > news >正文

Leetcode-2272. Substring With Largest Variance [C++][Java]

目录

一、题目描述

二、解题思路

【C++】

【Java】


Leetcode-2272. Substring With Largest Variancehttps://leetcode.com/problems/substring-with-largest-variance/description/2272. 最大波动的子字符串 - 力扣(LeetCode)2272. 最大波动的子字符串 - 字符串的 波动 定义为子字符串中出现次数 最多 的字符次数与出现次数 最少 的字符次数之差。给你一个字符串 s ,它只包含小写英文字母。请你返回 s 里所有 子字符串的 最大波动 值。子字符串 是一个字符串的一段连续字符序列。 示例 1:输入:s = "aababbb"输出:3解释:所有可能的波动值和它们对应的子字符串如以下所示:- 波动值为 0 的子字符串:"a" ,"aa" ,"ab" ,"abab" ,"aababb" ,"ba" ,"b" ,"bb" 和 "bbb" 。- 波动值为 1 的子字符串:"aab" ,"aba" ,"abb" ,"aabab" ,"ababb" ,"aababbb" 和 "bab" 。- 波动值为 2 的子字符串:"aaba" ,"ababbb" ,"abbb" 和 "babb" 。- 波动值为 3 的子字符串 "babbb" 。所以,最大可能波动值为 3 。示例 2:输入:s = "abcde"输出:0解释:s 中没有字母出现超过 1 次,所以 s 中每个子字符串的波动值都是 0 。 提示: * 1 <= s.length <= 104 * s  只包含小写英文字母。https://leetcode.cn/problems/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.

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.

二、解题思路

  • 时间复杂度:O(n * 26)
  • 空间复杂度:O(1)

【C++】

class Solution {
public:
    int largestVariance(string s) {
        int f0[26][26]{}, f1[26][26];
        memset(f1, -0x3f, sizeof(f1));
        int res = 0;
        for (auto& ch : s) {
            int a = ch - 'a';
            for (int b = 0; b < 26; ++b) {
                if (a == b) {continue;}
                f0[a][b] = max(f0[a][b], 0) + 1; // a为主频次(允许不含b)时的频次差
                f1[a][b]++; // a为主频次时的频次差,含b时有效;不含b时为很小的负数值
                f1[b][a] = f0[b][a] = max(f0[b][a], 0) - 1; // b为主频次(这里一定含a)时的频次差
                res = max(max(res, f1[a][b]), f1[b][a]);
            }
        }
        return res;
    }
};

【Java】

class Solution {
    public int largestVariance(String s) {
        int f0[][] = new int[26][26], f1[][] = new int[26][26];
        for (int[] row : f1) {Arrays.fill(row, Integer.MIN_VALUE);}
        int res = 0;
        for (char ch : s.toCharArray()) {
            int a = ch - 'a';
            for (int b = 0; b < 26; ++b) {
                if (a == b) {continue;}
                f0[a][b] = Math.max(f0[a][b], 0) + 1;
                f1[a][b]++;
                f1[b][a] = f0[b][a] = Math.max(f0[b][a], 0) - 1;
                res = Math.max(Math.max(res, f1[a][b]), f1[b][a]);
            }
        }
        return res;
    }
}

相关文章:

  • AI第一天 自我理解笔记--生成文本概率Top-k p 束搜索 贪心搜索温度
  • Python 鼠标轨迹算法 - 防止游戏检测
  • OSPF-4 2类LSA NetworkLSA
  • airtest用法
  • 欧税通:日本JCT标准申报的具体税率是多少?
  • N皇后问题——dfs解法(回溯+减枝+深搜)
  • Unreal Engine 5.4 (虚幻引擎5)中动画制作快捷键大全
  • 自动化测试脚本
  • 深入解析 Kubernetes Worker Node 组件
  • SpringBoot 第一课(Ⅲ) 配置类注解
  • 经历过的IDEA+Maven+JDK一些困惑
  • DNS服务器的简单搭建方法
  • Linux 上常见的解压缩命令
  • STM32F407——RTC实时时钟
  • 从Excel文件中读取数据
  • 字符串算法题
  • 在 Kaggle 中绘制中文乱码解决
  • 相对论-空间和时间(2)
  • 神聖的綫性代數速成例題5. 矩陣運算的定義、轉置的性質、方陣多項式的概念
  • Android 英文文章选词
  • 自己设计logo网站/seo云优化外包
  • 网站seo推广的方法/广告网站
  • 在excel中怎么做邮箱网站/潍坊网站开发公司
  • 轻量服务器wordpress/南宁seo优化公司
  • 四方区企业型网站建设/引流推广犯法吗
  • 专门做相声的网站/什么网站可以发布广告