📌  相关文章
📜  要与频率大于其他字符之和的字符连接的字符串计数

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

要与频率大于其他字符之和的字符连接的字符串计数

给定一个包含N个字符串的数组arr[] ,任务是找到可以连接的最大字符串数,使得一个字符的频率大于所有其他字符的频率之和。

例子:

方法:要解决此问题,请按照以下步骤操作:

  1. 迭代所有字符,即从“a”到“z”,并在每次迭代中找到该字符在所有字符串中的净频率。此处的净频率可以通过从中减去所有其他频率来计算,这意味着如果净频率大于 0,则该字符的频率大于所有其他频率的总和。将这些频率存储在向量v中。
  2. 现在,按降序对 v 进行排序。
  3. 然后对每个字符求最大可以组合的字符串个数,使得该字符出现的频率大于其他频率的总和。
  4. 返回最大可能的答案。

下面是上述方法的实现:

C++
// C++ implementation for the above approach
 
#include 
using namespace std;
 
// Function of find the
// frequency of all characters after
// reducing the sum of
// other frequencies in strings
vector frequency(vector& arr,
                      int ch)
{
 
    // Vector to store frequency
    vector v;
 
    // Iterate over the array of strings
    for (int i = 0; i < arr.size(); i++) {
 
        string s = arr[i];
 
        // Variable to store frequencies
        int net_freq = 0;
 
        // Iterate over the string
        for (auto x : s) {
            // If x is equal
            // to current character
            // increment net_freq by 1
            if (x == ch)
                net_freq++;
 
            // Else decrement net_freq by 1
            else
                net_freq--;
        }
 
        // After the iteration of string
        // store the frequency in vector
        v.push_back(net_freq);
    }
 
    return v;
}
 
// Function to find
// the longest string that
// can be made from
// a given vector of strings
int longestConcatenatedStr(
    vector& arr)
{
    // Variable to store maximum count
    int mx = 0;
 
    // Iterate over all alphabets
    for (char ch = 'a'; ch <= 'z'; ch++) {
 
        // Vector to store the
        // net_frequency of character
        // ch after reducing
        // the sum of all other
        // frequencies in all strings
        vector v = frequency(arr, ch);
 
        // Sort the vector in decreasing order
        sort(v.begin(), v.end(),
             greater());
 
        // Variable to store answer
        int ans = 0;
 
        int sum = 0;
        for (auto x : v) {
            sum += x;
 
            // If sum is greater than 0
            // increment ans by 1
            if (sum > 0) {
                ans++;
            }
        }
 
        // Keep track of the maximum one
        mx = max(mx, ans);
    }
    // Return the maximum value
    return mx;
}
 
// Driver Code
int main()
{
    vector arr
        = { "abac", "bacbc", "aacab" };
 
    cout << longestConcatenatedStr(arr);
 
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
 
class GFG {
  static String[] arr = { "abac", "bacbc", "aacab" };
 
  // Function of find the
  // frequency of all characters after
  // reducing the sum of
  // other frequencies in Strings
  static Vector frequency(int ch)
  {
 
    // Vector to store frequency
    Vector v = new Vector<>();
 
    // Iterate over the array of Strings
    for (int i = 0; i < arr.length; i++) {
 
      String s = arr[i];
 
      // Variable to store frequencies
      int net_freq = 0;
 
      // Iterate over the String
      for (char x : s.toCharArray())
      {
 
        // If x is equal
        // to current character
        // increment net_freq by 1
        if (x == ch)
          net_freq++;
 
        // Else decrement net_freq by 1
        else
          net_freq--;
      }
 
      // After the iteration of String
      // store the frequency in vector
      v.add(net_freq);
    }
 
    return v;
  }
 
  // Function to find
  // the longest String that
  // can be made from
  // a given vector of Strings
  static int longestConcatenatedStr()
  {
 
    // Variable to store maximum count
    int mx = 0;
 
    // Iterate over all alphabets
    for (char ch = 'a'; ch <= 'z'; ch++) {
 
      // Vector to store the
      // net_frequency of character
      // ch after reducing
      // the sum of all other
      // frequencies in all Strings
      Vector v = frequency(ch);
 
      // Sort the vector in decreasing order
      Collections.sort(v);
      Collections.reverse(v);
      // Variable to store answer
      int ans = 0;
 
      int sum = 0;
      for (int x : v) {
        sum += x;
 
        // If sum is greater than 0
        // increment ans by 1
        if (sum > 0) {
          ans++;
        }
      }
 
      // Keep track of the maximum one
      mx = Math.max(mx, ans);
    }
 
    // Return the maximum value
    return mx;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
 
    System.out.print(longestConcatenatedStr());
  }
}
 
// This code is contributed by Rajput-Ji


Python3
# python implementation for the above approach
 
# Function of find the
# frequency of all characters after
# reducing the sum of
# other frequencies in strings
def frequency(arr, ch):
 
    # Vector to store frequency
    v = []
 
    # Iterate over the array of strings
    for i in range(0, len(arr)):
 
        s = arr[i]
 
        # Variable to store frequencies
        net_freq = 0
 
        # Iterate over the string
        for x in s:
           
            # If x is equal
            # to current character
            # increment net_freq by 1
            if (x == ch):
                net_freq += 1
 
            # Else decrement net_freq by 1
            else:
                net_freq -= 1
 
        # After the iteration of string
        # store the frequency in vector
        v.append(net_freq)
 
    return v
 
 
# Function to find
# the longest string that
# can be made from
# a given vector of strings
def longestConcatenatedStr(arr):
 
    # Variable to store maximum count
    mx = 0
 
    # Iterate over all alphabets
    for ch in range(0, 26):
 
        # Vector to store the
        # net_frequency of character
        # ch after reducing
        # the sum of all other
        # frequencies in all strings
        v = frequency(arr, chr(ch + ord('a')))
 
        # Sort the vector in decreasing order
        v.sort(reverse=True)
 
        # Variable to store answer
        ans = 0
 
        sum = 0
        for x in v:
            sum += x
 
            # If sum is greater than 0
            # increment ans by 1
            if (sum > 0):
                ans += 1
 
        # Keep track of the maximum one
        mx = max(mx, ans)
 
    # Return the maximum value
    return mx
 
# Driver Code
if __name__ == "__main__":
 
    arr = ["abac", "bacbc", "aacab"]
 
    print(longestConcatenatedStr(arr))
 
# This code is contributed by rakeshsahni


Javascript


输出
2

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