LeetCode(1297):子串的最大出现次数 Maximum Number of Occurrences of a Substring(Java)

2021.1.4 LeetCode 从零单刷个人笔记整理(持续更新)

github:https://github.com/ChopinXBP/LeetCode-Babel

利用滑动窗口的思路可以做,但是本题有一个思维技巧在于:只需要创建minSize的滑动窗口即可,如果长度为maxSize的子串出现了N次,那么长度为minSize的子串也会出现N次。而题目只要求最大数量的子串即可,没有长度要求。


传送门:子串的最大出现次数

Given a string s, return the maximum number of ocurrences of any substring under the following rules:

The number of unique characters in the substring must be less than or equal to maxLetters.

The substring size must be between minSize and maxSize inclusive.

给你一个字符串 s ,请你返回满足以下条件且出现次数最大的 任意 子串的出现次数:

子串中不同字母的数目必须小于等于 maxLetters 。

子串的长度必须大于等于 minSize 且小于等于 maxSize 。

示例 1:
输入:s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
输出:2
解释:子串 "aab" 在原字符串中出现了 2 次。
它满足所有的要求:2 个不同的字母,长度为 3 (在 minSize 和 maxSize 范围内)。

示例 2:
输入:s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
输出:2
解释:子串 "aaa" 在原字符串中出现了 2 次,且它们有重叠部分。

示例 3:
输入:s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
输出:3

示例 4:
输入:s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
输出:0

提示:
1 <= s.length <= 10^5
1 <= maxLetters <= 26
1 <= minSize <= maxSize <= min(26, s.length)
s只包含小写英文字母。


package Problems;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * Given a string s, return the maximum number of ocurrences of any substring under the following rules:
 * The number of unique characters in the substring must be less than or equal to maxLetters.
 * The substring size must be between minSize and maxSize inclusive.
 * 给你一个字符串 s ,请你返回满足以下条件且出现次数最大的 任意 子串的出现次数:
 * 子串中不同字母的数目必须小于等于 maxLetters 。
 * 子串的长度必须大于等于 minSize 且小于等于 maxSize 。
 *
 */

public class MaximumNumberOfOccurrencesOfASubstring {
   
    /**
     * 原思路解法:滑动窗口
     */
    public int maxFreq0(String s, int maxLetters, int minSize, int maxSize) {
   
        int[] words = new int[26];
        int curNum = 0;
        for(int i = 0; i < minSize - 1; i++) {
   
            int
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值