Find count of Alphabetic and Alphanumeric strings from given Array of Strings
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 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<String, Integer> mp1 = new HashMap<String, Integer>(); Map<String, Integer> mp2 = new HashMap<String, Integer>(); 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<String, Integer> entry : mp1.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } // Print the frequencies of // alphanumeric strings for (Map.Entry<String, Integer> 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. |
C#
// C# program to implement above approach using System; using System.Collections; using System.Collections.Generic; class GFG { // Function to find frequency static void find_freq(String[] v, int n) { // Take an hash map function // to count frequency SortedDictionary<String, int > mp1 = new SortedDictionary<String, int >(); SortedDictionary<String, int > mp2 = new SortedDictionary<String, int >(); int count1 = 0, count2 = 0; bool 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][j] >= '0' && v[i][j] <= '9' ) { flag = false ; break ; } } // For alphabetic string if (flag) { count1++; if (mp1.ContainsKey(v[i])) { mp1[v[i]] += 1; } else { mp1.Add(v[i], 1); } } // For alphanumeric string else { count2++; if (mp2.ContainsKey(v[i])) { mp2[v[i]] += 1; } else { mp2.Add(v[i], 1); } } } Console.WriteLine(count1 + " " + count2); // Print the frequencies of // alphabetic strings foreach (KeyValuePair<String, int > entry in mp1) { Console.WriteLine(entry.Key + ": " + entry.Value); } // Print the frequencies of // alphanumeric strings foreach (KeyValuePair<String, int > entry in mp2) { Console.WriteLine(entry.Key + ": " + entry.Value); } } // Driver code public static void Main( string [] args) { int N = 5; String[] arr = new String[]{ "abcd" , "mak87s" , "abcd" , "kakjdj" , "laojs7s6" }; // Function call find_freq(arr, N); } } // This code is contributed by subhamgoyal2014. |
Javascript
<script> // JavaScript code to implement the above approach // Function to find frequency const find_freq = (v, n) => // Take an hash map function // to count frequency let mp1 = {}, mp2 = {}; let count1 = 0, count2 = 0; let flag = false ; for (let i = 0; i < n; i++) { flag = true ; for (let j = 0; j < v[i].length; 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++; if (v[i] in mp1) mp1[v[i]] += 1 else mp1[v[i]] = 1; } // For alphanumeric string else { count2++; if (v[i] in mp2) mp2[v[i]] += 1; else mp2[v[i]] = 1; } } document.write(`${count1} ${count2}<br/>`); // Print the frequencies of // alphabetic strings for (let it in mp1) { document.write(`${it}: ${mp1[it]}<br/>`); } // Print the frequencies of // alphanumeric strings for (let it in mp2) { document.write(`${it}: ${mp2[it]}<br/>`); } } // Drive code let N = 5; let arr = [ "abcd" , "mak87s" , "abcd" , "kakjdj" , "laojs7s6" ]; // Function call find_freq(arr, N); // This code is contributed by rakeshsahni </script> |
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)
Please Login to comment...