The Wayback Machine - https://web.archive.org/web/20220218183137/https://www.geeksforgeeks.org/find-count-of-alphabetic-and-alphanumeric-strings-from-given-array-of-strings/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find count of Alphabetic and Alphanumeric strings from given Array of Strings

  • Last Updated : 18 Feb, 2022

Given an array of strings arr[] of size N. Each of the strings contains lowercase English letters or numbers only. The task is to find the frequency of alphabetic and alphanumeric strings in the array.

Note: If any string has atleast one number in it, the string is an alphanumeric string.

Examples:

Input: arr[] = {“abcd”, “mak87s”, “abcd”, “kakjdj”, “laojs7s6”}
Output: 3 2
“abcd”: 2
“kakjdj”: 1
“mak87s”: 1
“laojs7s6”: 1
Explanation: From the given input the strings which have only alphabets are
“abcd”, “kakjdj” and the remaining strings contains numbers. So 3 alphabetic and two alphanumeric strings in total.
These two strings have frequency of 2 and 1 and the other two strings have frequency of 1 each.

Input: arr[] = {“defr3t”, “lsk4dk”, “njdcd”}
Output: 1 2
“njdcd”: 1
“defr3t”: 1
“lsk4dk”: 1

 

Approach: The approach to solving this problem is based upon hashing technique. Follow the steps mentioned below to solve the problem.

  • Take a hash map function to count the frequency
  • Now iterate a from 0 to N-1 and then store.
    • Now iterate another for loop inside the first for loop(nested loop) from 0 to size of that string.
    • Now check whether each element is an alphabet or not
      • If yes increment the count of alphabetic strings and increase the frequency of that string by 1.
      • Otherwise, increment the count of alphanumeric strings and increase the frequency of that string by 1.
  • Print the total count of alphanumeric and alphabetic strings and frequencies of each string separately.

Below is the implementation of the above approach:

C++




// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find frequency
void find_freq(string v[], int n)
{
    // Take an hash map function
    // to count frequency
    map<string, int> 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 whther 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;
}
Output
3 2
abcd: 2
kakjdj: 1
laojs7s6: 1
mak87s: 1

Time Complexity: O(N * M) where M is the maximum length of a string
Auxiliary Space: O(N)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!