📌  相关文章
📜  给定字符串的子串计数,每个字符的频率最多为 K

📅  最后修改于: 2022-05-13 01:57:06.768000             🧑  作者: Mango

给定字符串的子串计数,每个字符的频率最多为 K

给定一个字符串str ,任务是计算给定字符串的子字符串数,使得字符串中每个元素的频率几乎为K

例子:

方法:给定的问题可以使用两指针技术来解决。遍历 [0, N) 范围内字符串的每个字符,并保持每个字符在无序映射中出现的频率。创建一个变量ptr ,它存储当前窗口起点的索引。最初, ptr 的值为0 。对于索引i ,如果str[i]的频率小于或等于K ,则将(i – ptr + 1)添加到子字符串计数中,否则,递增 ptr 的值直到str[i] > K

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the count of
// substrings such that frequency
// of each character is atmost K
int cntSubstr(string str, int K)
{
    // Stores the size of string
    int N = str.size();
 
    // Stores the final count
    int ans = 0;
 
    // Stores the starting index
    int ptr = 0;
 
    // Stores the frequency of
    // characters of string
    unordered_map m;
 
    // Loop to iterate through string
    for (int i = 0; i < N; i++) {
 
        // Increment the frequency of
        // the current character
        m[str[i]]++;
 
        // While the frequency of char is
        // greater than K, increment ptr
        while (m[str[i]] > K && ptr <= i) {
            m[str[ptr]]--;
            ptr++;
        }
 
        // Update count
        ans += (i - ptr + 1);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    string str = "abab";
    int K = 1;
 
    cout << cntSubstr(str, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the count of
// substrings such that frequency
// of each character is atmost K
static int cntSubstr(String str, int K)
{
     
    // Stores the size of string
    int N = str.length();
 
    // Stores the final count
    int ans = 0;
 
    // Stores the starting index
    int ptr = 0;
 
    // Stores the frequency of
    // characters of string
    HashMap m = new HashMap();
 
    // Loop to iterate through string
    for(int i = 0; i < N; i++)
    {
         
        // Increment the frequency of
        // the current character
        int count = 0;
        if (m.containsKey(str.charAt(i)))
        {
            count = m.get(str.charAt(i));
        }
        m.put(str.charAt(i), count + 1);
 
        // While the frequency of char is
        // greater than K, increment ptr
        while (m.get(str.charAt(i)) > K && ptr <= i)
        {
            m.put(str.charAt(ptr),
                  m.get(str.charAt(ptr)) - 1);
            ptr++;
        }
 
        // Update count
        ans += (i - ptr + 1);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    String str = "abab";
    int K = 1;
 
    System.out.println(cntSubstr(str, K));
}
}
 
// This code is contributed by ukasp


Python3
# Python Program to implement
# the above approach
 
# Function to find the count of
# substrings such that frequency
# of each character is atmost K
def cntSubstr(str, K):
   
    # Stores the size of string
    N = len(str)
 
    # Stores the final count
    ans = 0
 
    # Stores the starting index
    ptr = 0
 
    # Stores the frequency of
    # characters of string
    m = {}
 
    # Loop to iterate through string
    for i in range(N) :
 
        # Increment the frequency of
        # the current character
        if (str[i] in m):
            m[str[i]] += 1
        else:
            m[str[i]] = 1
 
        # While the frequency of char is
        # greater than K, increment ptr
        while (m[str[i]] > K and ptr <= i):
            m[str[ptr]] -= 1
            ptr += 1
         
        # Update count
        ans += (i - ptr + 1)
 
    # Return Answer
    return ans
 
# Driver Code
str = "abab"
K = 1
print(cntSubstr(str, K))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG
{
   
// Function to find the count of
// substrings such that frequency
// of each character is atmost K
static int cntSubstr(string str, int K)
{
   
    // Stores the size of string
    int N = str.Length;
 
    // Stores the final count
    int ans = 0;
 
    // Stores the starting index
    int ptr = 0;
 
    // Stores the frequency of
    // characters of string
    Dictionary m =
          new Dictionary();
           
    // Loop to iterate through string
    for (int i = 0; i < N; i++) {
 
        // Increment the frequency of
        // the current character
        int count = 0;
        if (m.ContainsKey(str[i])) {
                 
            count = m[str[i]];
        }
        m[str[i]] = count + 1;
 
        // While the frequency of char is
        // greater than K, increment ptr
        while (m[str[i]] > K && ptr <= i) {
            m[str[ptr]]--;
            ptr++;
        }
 
        // Update count
        ans += (i - ptr + 1);
    }
 
    // Return Answer
    return ans;
}
 
// Driver Code
public static void Main()
{
    string str = "abab";
    int K = 1;
     
    Console.Write(cntSubstr(str, K));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
7

时间复杂度: O(N)
辅助空间: O(N)