LeetCode-1297. Maximum Number of Occurrences of a Substring

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.

 

Example 1:

Input: s = "aababcaab", maxLetters = 2, minSize = 3, maxSize = 4
Output: 2
Explanation: Substring "aab" has 2 ocurrences in the original string.
It satisfies the conditions, 2 unique letters and size 3 (between minSize and maxSize).

Example 2:

Input: s = "aaaa", maxLetters = 1, minSize = 3, maxSize = 3
Output: 2
Explanation: Substring "aaa" occur 2 times in the string. It can overlap.

Example 3:

Input: s = "aabcabcab", maxLetters = 2, minSize = 2, maxSize = 3
Output: 3

Example 4:

Input: s = "abcde", maxLetters = 2, minSize = 3, maxSize = 3
Output: 0

 

Constraints:

  • 1 <= s.length <= 10^5
  • 1 <= maxLetters <= 26
  • 1 <= minSize <= maxSize <= min(26, s.length)
  • s only contains lowercase English letters.

题解:

完全可以不需要maxSize,因为minSize肯定包含了maxSize的子串。

字典树做法:

class Solution {
private:
    struct TrieTree {
        TrieTree* child[26];
        int count;
        unordered_set<char> letters;
        TrieTree() {
            for (int i = 0; i < 26; i++) {
                child[i] = NULL;
            }
            count = 0;
        }
    };
    void insert(TrieTree *root, string word) {
        TrieTree *t = root;
        unordered_set<char> letters;
        for (int i = 0; i < word.size(); i++) {
            letters.insert(word[i]);
            if (t->child[word[i]- 'a'] == NULL) {
                t->child[word[i] - 'a'] = new TrieTree();
            }
            t = t->child[word[i] - 'a'];
            t->letters = letters;
        }
        t->letters = letters;
        t->count++;
    }
    void visit(TrieTree *root, int &res, int maxLetters) {
        if (root != NULL) {
            if (root->letters.size() <= maxLetters) {
                res = max(res, root->count);
            }
            for (int i = 0; i < 26; i++) {
                if (root->child[i] != NULL) {
                    visit(root->child[i], res, maxLetters);
                }
            }
        }
    }
public:
    int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
        TrieTree *root = new TrieTree();
        for (int i = 0; i <= s.length() - minSize; i++) {
            string sub = s.substr(i, minSize);
            insert(root, sub);
        }
        int res = 0;
        visit(root, res, maxLetters);
        return res;
    }
};

map做法:

class Solution {
public:
    int calLetters(string s) {
        unordered_set<char> st;
        for (int i = 0; i < s.length(); i++) {
            st.insert(s[i]);
        }
        return st.size();
    }
    int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
        unordered_map<string, int> m; 
        for (int i = 0; i < s.length(); i++) {
            if (i + minSize <= s.length()) {
                string sub = s.substr(i, minSize);
                m[sub]++;
            }
        }
        int res = 0;
        for (auto it = m.begin(); it != m.end(); it++) {
            if (calLetters(it->first) <= maxLetters) {
                res = max(res, it->second);
            }
        }
        return res;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值