📌  相关文章
📜  可以从第一个字符串的字符形成第二个字符串的次数

📅  最后修改于: 2021-04-22 07:08:18             🧑  作者: Mango

给定两个字符串strpatt ,任务是找到使用str的字符可以形成patt的次数。

例子:

方法:计算strpatt所有字符的频率,并将它们分别存储在数组strFreq []pattFreq []中。现在,出现在patt中的任何字符ch都可以使用最大strFreq [ch] / pattFreq [ch]单词,并且必须在patt的所有字符中使用该值的最小值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int MAX = 26;
  
// Function to update the freq[] array
// to store the frequencies of
// all the characters of str
void updateFreq(string str, int freq[])
{
    int len = str.length();
  
    // Update the frequency of the characters
    for (int i = 0; i < len; i++) {
        freq[str[i] - 'a']++;
    }
}
  
// Function to return the maximum count
// of times patt can be formed
// using the characters of str
int maxCount(string str, string patt)
{
  
    // To store the frequencies of
    // all the characters of str
    int strFreq[MAX] = { 0 };
    updateFreq(str, strFreq);
  
    // To store the frequencies of
    // all the characters of patt
    int pattFreq[MAX] = { 0 };
    updateFreq(patt, pattFreq);
  
    // To store the result
    int ans = INT_MAX;
  
    // For every character
    for (int i = 0; i < MAX; i++) {
  
        // If the current character
        // doesn't appear in patt
        if (pattFreq[i] == 0)
            continue;
  
        // Update the result
        ans = min(ans, strFreq[i] / pattFreq[i]);
    }
  
    return ans;
}
  
// Driver code
int main()
{
    string str = "geeksforgeeks";
    string patt = "geeks";
  
    cout << maxCount(str, patt);
  
    return 0;
}


Java
// Java implementation of the approach
  
class GFG
{
  
static int MAX = 26;
  
// Function to update the freq[] array
// to store the frequencies of
// all the characters of str
static void updateFreq(String str, int freq[])
{
    int len = str.length();
  
    // Update the frequency of the characters
    for (int i = 0; i < len; i++) 
    {
        freq[str.charAt(i) - 'a']++;
    }
}
  
// Function to return the maximum count
// of times patt can be formed
// using the characters of str
static int maxCount(String str, String patt)
{
  
    // To store the frequencies of
    // all the characters of str
    int []strFreq = new int[MAX];
    updateFreq(str, strFreq);
  
    // To store the frequencies of
    // all the characters of patt
    int []pattFreq = new int[MAX];
    updateFreq(patt, pattFreq);
  
    // To store the result
    int ans = Integer.MAX_VALUE;
  
    // For every character
    for (int i = 0; i < MAX; i++) 
    {
  
        // If the current character
        // doesn't appear in patt
        if (pattFreq[i] == 0)
            continue;
  
        // Update the result
        ans = Math.min(ans, strFreq[i] / pattFreq[i]);
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    String str = "geeksforgeeks";
    String patt = "geeks";
  
    System.out.print(maxCount(str, patt));
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
MAX = 26
  
# Function to update the freq[] array
# to store the frequencies of
# all the characters of strr
def updateFreq(strr, freq):
    lenn = len(strr)
  
    # Update the frequency of the characters
    for i in range(lenn):
        freq[ord(strr[i]) - ord('a')] += 1
  
# Function to return the maximum count
# of times patt can be formed
# using the characters of strr
def maxCount(strr, patt):
  
    # To store the frequencies of
    # all the characters of strr
    strrFreq = [0 for i in range(MAX)]
    updateFreq(strr, strrFreq)
  
    # To store the frequencies of
    # all the characters of patt
    pattFreq = [0 for i in range(MAX)]
    updateFreq(patt, pattFreq)
  
    # To store the result
    ans = 10**9
  
    # For every character
    for i in range(MAX):
  
        # If the current character
        # doesn't appear in patt
        if (pattFreq[i] == 0):
            continue
  
        # Update the result
        ans = min(ans, strrFreq[i] // pattFreq[i])
  
    return ans
  
# Driver code
strr = "geeksforgeeks"
patt = "geeks"
  
print(maxCount(strr, patt))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
  
class GFG
{
  
static int MAX = 26;
  
// Function to update the []freq array
// to store the frequencies of
// all the characters of str
static void updateFreq(String str, int []freq)
{
    int len = str.Length;
  
    // Update the frequency of the characters
    for (int i = 0; i < len; i++) 
    {
        freq[str[i] - 'a']++;
    }
}
  
// Function to return the maximum count
// of times patt can be formed
// using the characters of str
static int maxCount(String str, String patt)
{
  
    // To store the frequencies of
    // all the characters of str
    int []strFreq = new int[MAX];
    updateFreq(str, strFreq);
  
    // To store the frequencies of
    // all the characters of patt
    int []pattFreq = new int[MAX];
    updateFreq(patt, pattFreq);
  
    // To store the result
    int ans = int.MaxValue;
  
    // For every character
    for (int i = 0; i < MAX; i++) 
    {
  
        // If the current character
        // doesn't appear in patt
        if (pattFreq[i] == 0)
            continue;
  
        // Update the result
        ans = Math.Min(ans, strFreq[i] / pattFreq[i]);
    }
    return ans;
}
  
// Driver code
public static void Main(String[] args)
{
    String str = "geeksforgeeks";
    String patt = "geeks";
  
    Console.Write(maxCount(str, patt));
}
}
  
// This code is contributed by 29AjayKumar


输出:
2