📜  从给定的字符串数组中查找字母和字母数字字符串的计数

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

从给定的字符串数组中查找字母和字母数字字符串的计数

给定一个大小为N的字符串数组arr[] 。每个字符串仅包含小写英文字母或数字。任务是找出数组中字母字母数字字符串的频率。

注意:如果任何字符串中至少有一个数字,则该字符串是字母数字字符串。

例子:

方法:解决这个问题的方法是基于散列技术。请按照以下步骤解决问题。

  • 取一个hash map函数来统计频率
  • 现在从 0 到 N-1 迭代 a然后存储。
    • 现在在第一个 for 循环(嵌套循环)中迭代另一个 for 循环,从 0 到该字符串的大小。
    • 现在检查每个元素是否是字母
      • 如果是,则增加字母字符串的计数并将该字符串的频率增加 1。
      • 否则,增加字母数字字符串的计数并将该字符串的频率增加 1。
  • 分别打印字母数字和字母字符串的总数以及每个字符串的频率。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to find frequency
void find_freq(string v[], int n)
{
    // Take an hash map function
    // to count frequency
    map mp1, mp2;
 
    int count1 = 0, count2 = 0;
    bool flag;
    for (int i = 0; i < n; i++) {
        flag = true;
        for (int j = 0; j < v[i].size(); j++) {
 
            // Check whether the string has
            // numbers or not
            if (v[i][j] >= '0'
                && v[i][j] <= '9') {
                flag = false;
                break;
            }
        }
 
        // For alphabetic string
        if (flag) {
            count1++;
            mp1[v[i]]++;
        }
 
        // For alphanumeric string
        else {
            count2++;
            mp2[v[i]]++;
        }
    }
    cout << count1 << " " << count2 << endl;
 
    // Print the frequencies of
    // alphabetic strings
    for (auto it : mp1) {
        cout << it.first << ": "
             << it.second << endl;
        ;
    }
 
    // Print the frequencies of
    // alphanumeric strings
    for (auto it : mp2) {
        cout << it.first << ": "
             << it.second << endl;
        ;
    }
}
 
// Drive code
int main()
{
    int N = 5;
    string arr[] = { "abcd", "mak87s", "abcd",
                     "kakjdj", "laojs7s6" };
 
    // Function call
    find_freq(arr, N);
    return 0;
}


Java
// Java code for the above approach
import java.util.*;
 
class GFG {
 
  // Function to find frequency
  static void find_freq(String v[], int n)
  {
     
    // Take an hash map function
    // to count frequency
    Map mp1 = new HashMap();
    Map mp2 = new HashMap();
 
    int count1 = 0, count2 = 0;
    Boolean flag;
    for (int i = 0; i < n; i++) {
      flag = true;
      for (int j = 0; j < v[i].length(); j++) {
 
        // Check whether the string has
        // numbers or not
        if (v[i].charAt(j) >= '0'
            && v[i].charAt(j) <= '9') {
          flag = false;
          break;
        }
      }
 
      // For alphabetic string
      if (flag) {
        count1++;
        if (mp1.containsKey(v[i]))
        {
          mp1.put(v[i], mp1.get(v[i]) + 1);
        }
        else
        {
          mp1.put(v[i], 1);
        }
      }
 
      // For alphanumeric string
      else {
        count2++;
        if (mp2.containsKey(v[i]))
        {
          mp2.put(v[i], mp2.get(v[i]) + 1);
        }
        else
        {
          mp2.put(v[i], 1);
        }
      }
    }
    System.out.println(count1 + " " + count2);
 
    // Print the frequencies of
    // alphabetic strings
    for (Map.Entry entry : mp1.entrySet())
    {
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
 
    // Print the frequencies of
    // alphanumeric strings
    for (Map.Entry entry : mp2.entrySet())
    {
      System.out.println(entry.getKey() + ":" + entry.getValue());
    }
 
  }
 
  // Drive code
  public static void main (String[] args) {
    int N = 5;
    String arr[] = { "abcd", "mak87s", "abcd",
                    "kakjdj", "laojs7s6" };
 
    // Function call
    find_freq(arr, N);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code to implement the above approach
 
# Function to find frequency
def find_freq(v, n):
   
    # Take an hash map function
    # to count frequency
    mp1 = {}
    mp2 = {}
 
    count1 = 0
    count2 = 0
    flag = False
    for i in range(n):
        flag = True
        for j in range(len(v[i])):
 
            # Check whether the string has
            # numbers or not
            if(v[i][j] >= '0' and v[i][j] <= '9'):
                flag = False
                break
 
        # For alphabetic string
        if (flag):
            count1 = count1+1
            if v[i] in mp1:
                mp1[v[i]] = mp1[v[i]]+1
            else :
                mp1[v[i]] = 1
 
        # For alphanumeric string
        else :
            count2 = count2+1
            if v[i] in mp1:
                mp2[v[i]] = mp2[v[i]]+1
            else :
                mp2[v[i]] = 1
    print(f"{count1} {count2}")
 
    # Print the frequencies of
    # alphabetic strings
    for key,value in mp1.items():
        print(f"{key} : {value}")
 
    # Print the frequencies of
    # alphanumeric strings
    for key,value in mp2.items():
        print(f"{key} : {value}")
 
# Driver code
N = 5
arr = [ "abcd", "mak87s", "abcd", "kakjdj", "laojs7s6" ];
 
# Function call
find_freq(arr, N);
 
# This code is contributed by shinjanpatra.


Javascript



输出
3 2
abcd: 2
kakjdj: 1
laojs7s6: 1
mak87s: 1

时间复杂度: O(N * M) 其中 M 是字符串的最大长度
辅助空间: 在)