Given string str, the task is to find the frequency of all digits (0-9) in a string created by concatenating the ASCII values of each character of the given string str.
Example:
Input: str = “GeeksForGeeks”
Output: 7 21 0 0 1 2 0 5 0 0
Explanation: The array of ASCII values of all characters of the given string is {71, 101, 101, 107, 115, 70, 111, 114, 71, 101, 101, 107, 115}. Hence, the frequency of digit 0 in the array is freq[0] = 7. Similarly, freq[1] = 21, freq[2] = 4, freq[3] = 0, and so on.Input: str = “Computer123”
Output: 3 15 1 0 2 2 2 2 0 2
Approach: The given problem is an implementation-based problem and can be solved by following the given steps:
- Create a string asc, which stores the ASCII value of each character.
- Traverse the given string str, find the ASCII value of each character and append it into asc using the to string inbuilt function.
- Traverse the string asc and update the frequency of the current digit stored in a frequency array.
- Print the frequency of each digit.
Below is the implementation of the above approach:
// C++ program of the above approach #include <bits/stdc++.h> using namespace std;
// Function to find the frequency of // all digits in the array of ASCII // values of all characters of str void digitFreq(string str)
{ // Stores the ASCII string
string asc = "" ;
// Loop to traverse string
for ( auto x : str) {
// Append ASCII value of
// current string to asc
asc += to_string(( int )x);
}
// Stores frequency of digits
int freq[10] = {};
// Loop to traverse asc
for ( auto x : asc) {
freq[x - '0' ]++;
}
// Print frequency of each digit
for ( int i = 0; i < 10; i++) {
cout << freq[i] << " " ;
}
} // Driver Code int main()
{ string str;
str = "GeeksForGeeks" ;
digitFreq(str);
return 0;
} |
// Java implementation of the above approach import java.util.*;
public class GFG{
// Function to find the frequency of
// all digits in the array of ASCII
// values of all characters of str
static void digitFreq(String str)
{
// Stores the ASCII string
String asc = "" ;
char [] ch = str.toCharArray();
// Loop to traverse string
for ( int x : ch) {
// Append ASCII value of
// current string to asc
asc += Integer.toString(( int )x);
}
// Stores frequency of digits
int [] freq = new int [ 10 ];
// Loop to traverse asc
for ( int i = 0 ; i < asc.length(); i++) {
freq[asc.charAt(i) - '0' ]++;
}
// Print frequency of each digit
for ( int i = 0 ; i < 10 ; i++) {
System.out.print(freq[i] + " " );
}
}
// Driver Code
public static void main(String args[])
{
String str = "GeeksForGeeks" ;
digitFreq(str);
}
} // This code is contributed by Samim Hossain Mondal. |
# Python code for the above approach # Function to find the frequency of # all digits in the array of ASCII # values of all characters of str def digitFreq(s):
# Stores the ASCII string
asc = ""
# Loop to traverse string
for x in range ( len (s)):
# Append ASCII value of
# current string to asc
asc = asc + str ( int ( ord (s[x])))
# Stores frequency of digits
freq = [ 0 ] * 10
# Loop to traverse asc
for x in range ( len (asc)):
freq[ ord (asc[x]) - ord ( '0' )] = freq[ ord (asc[x]) - ord ( '0' )] + 1
# Print frequency of each digit
for i in range ( 10 ):
print (freq[i], end = " " )
# Driver Code s = "GeeksForGeeks"
digitFreq(s) # This code is contributed by Potta Lokesh |
// C# implementation of the above approach using System;
class GFG{
// Function to find the frequency of
// all digits in the array of ASCII
// values of all characters of str
static void digitFreq( string str)
{
// Stores the ASCII string
string asc = "" ;
// Loop to traverse string
foreach ( int x in str) {
// Append ASCII value of
// current string to asc
asc += (( int )x).ToString();
}
// Stores frequency of digits
int [] freq = new int [10];
// Loop to traverse asc
foreach ( int x in asc) {
freq[x - '0' ]++;
}
// Print frequency of each digit
for ( int i = 0; i < 10; i++) {
Console.Write(freq[i] + " " );
}
}
// Driver Code
public static void Main()
{
string str;
str = "GeeksForGeeks" ;
digitFreq(str);
}
} // This code is contributed by sanjoy_62. |
<script> // JavaScript program of the above approach
// Function to find the frequency of
// all digits in the array of ASCII
// values of all characters of str
const digitFreq = (str) => {
// Stores the ASCII string
let asc = "" ;
// Loop to traverse string
for (let x in str) {
// Append ASCII value of
// current string to asc
asc += str.charCodeAt(x).toString();
}
// Stores frequency of digits
let freq = new Array(10).fill(0);
// Loop to traverse asc
for (let x in asc) {
freq[asc.charCodeAt(x) - '0' .charCodeAt(0)]++;
}
// Print frequency of each digit
for (let i = 0; i < 10; i++) {
document.write(`${freq[i]} `);
}
}
// Driver Code
str = "GeeksForGeeks" ;
digitFreq(str);
// This code is contributed by rakeshsahni </script> |
7 21 0 0 1 2 0 5 0 0
Time Complexity: O(N)
Auxiliary Space: O(N)