Longest Substring with At Least K Repeating Characters LeetCode Solution

Last updated on October 5th, 2024 at 05:50 pm

Here, We see Longest Substring with At Least K Repeating Characters LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Topics

String

Companies

Baidu

Level of Question

Medium

Longest Substring with At Least K Repeating Characters LeetCode Solution

Longest Substring with At Least K Repeating Characters LeetCode Solution

Problem Statement

Given a string s and an integer k, return the length of the longest substring of s such that the frequency of each character in this substring is greater than or equal to k.

if no such substring exists, return 0.

Example 1:
Input: s = “aaabb”, k = 3
Output: 3
Explanation: The longest substring is “aaa”, as ‘a’ is repeated 3 times.

Example 2:
Input: s = “ababbc”, k = 2
Output: 5
Explanation: The longest substring is “ababb”, as ‘a’ is repeated 2 times and ‘b’ is repeated 3 times.

1. Longest Substring with At Least K Repeating Characters LeetCode Solution C++

class Solution {
public:
    int longestSubstring(string s, int k) {
        int n = s.length();
        if(n == 0 or n < k) return 0;
        if(k <= 1) return n;
        unordered_map<char,int> countMap;
        for(char c : s) countMap[c]++;
        int left =0;
        while(left < n && countMap[s[left]] >= k) left++;
        if(left >= n-1) return left;
        int l1 = longestSubstring(s.substr(0,left) ,k);
        while(left < n && countMap[s[left]] < k) left++;
        int l2 = left < n ? longestSubstring(s.substr(left),k) : 0;
        return max(l1,l2);
    }
};

2. Longest Substring with At Least K Repeating Characters LeetCode Solution Java

class Solution {
    public int longestSubstring(String s, int k) {
        if (s == null || s.length() == 0) return 0;
        char[] chars = new char[26];
        for (int i = 0; i < s.length(); i += 1) chars[s.charAt(i) - 'a'] += 1;
        boolean flag = true;
        for (int i = 0; i < chars.length; i += 1) {
            if (chars[i] < k && chars[i] > 0) flag = false;
        }
        if (flag == true) return s.length();
        int result = 0;
        int start = 0, cur = 0;
        while (cur < s.length()) {
            if (chars[s.charAt(cur) - 'a'] < k) {
                result = Math.max(result, longestSubstring(s.substring(start, cur), k));
                start = cur + 1;
            }
            cur++;
        }
        result = Math.max(result, longestSubstring(s.substring(start), k));
        return result;
    }
}

3. Longest Substring with At Least K Repeating Characters LeetCode Solution JavaScript

var longestSubstring = function(s, k) {
    let maxUnique = new Set(s).size;
    let max = 0;
    for (let curUnique = 1; curUnique <= maxUnique; curUnique++) {
        let start = 0, end = 0, atLeastK = 0, unique = 0, m = new Map();
        while (end < s.length) {    
            m.set(s[end], m.get(s[end]) + 1 || 1);
            if (m.get(s[end]) == 1) unique++;
            if (m.get(s[end]) == k) atLeastK++;
            while (unique > curUnique) {
                m.set(s[start], m.get(s[start]) - 1);
                if (m.get(s[start]) == k-1) atLeastK--;
                if (m.get(s[start]) == 0) unique--;
                start++;
            }
            if (unique == curUnique && unique == atLeastK) {
                max = Math.max(max, end - start + 1);
            } 
            end++;
        }
    }
    return max;
};

4. Longest Substring with At Least K Repeating Characters LeetCode Solution Python

class Solution(object):
    def longestSubstring(self, s, k):
        cnt = collections.Counter(s)
        st = 0
        maxst = 0
        for i, c in enumerate(s):
            if cnt[c] < k:
                maxst = max(maxst, self.longestSubstring(s[st:i], k))
                st = i + 1
        return len(s) if st == 0 else max(maxst, self.longestSubstring(s[st:], k))
Scroll to Top