Find frequency of each digit 0-9 by concatenating ASCII values of given string
Last Updated :
08 Feb, 2024
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++
#include <bits/stdc++.h>
using namespace std;
void digitFreq(string str)
{
string asc = "" ;
for ( auto x : str) {
asc += to_string(( int )x);
}
int freq[10] = {};
for ( auto x : asc) {
freq[x - '0' ]++;
}
for ( int i = 0; i < 10; i++) {
cout << freq[i] << " " ;
}
}
int main()
{
string str;
str = "GeeksForGeeks" ;
digitFreq(str);
return 0;
}
|
Java
import java.util.*;
public class GFG{
static void digitFreq(String str)
{
String asc = "" ;
char [] ch = str.toCharArray();
for ( int x : ch) {
asc += Integer.toString(( int )x);
}
int [] freq = new int [ 10 ];
for ( int i = 0 ; i < asc.length(); i++) {
freq[asc.charAt(i) - '0' ]++;
}
for ( int i = 0 ; i < 10 ; i++) {
System.out.print(freq[i] + " " );
}
}
public static void main(String args[])
{
String str = "GeeksForGeeks" ;
digitFreq(str);
}
}
|
Python3
def digitFreq(s):
asc = ""
for x in range ( len (s)):
asc = asc + str ( int ( ord (s[x])))
freq = [ 0 ] * 10
for x in range ( len (asc)):
freq[ ord (asc[x]) - ord ( '0' )] = freq[ ord (asc[x]) - ord ( '0' )] + 1
for i in range ( 10 ):
print (freq[i], end = " " )
s = "GeeksForGeeks"
digitFreq(s)
|
C#
using System;
class GFG{
static void digitFreq( string str)
{
string asc = "" ;
foreach ( int x in str) {
asc += (( int )x).ToString();
}
int [] freq = new int [10];
foreach ( int x in asc) {
freq[x - '0' ]++;
}
for ( int i = 0; i < 10; i++) {
Console.Write(freq[i] + " " );
}
}
public static void Main()
{
string str;
str = "GeeksForGeeks" ;
digitFreq(str);
}
}
|
Javascript
<script>
const digitFreq = (str) => {
let asc = "" ;
for (let x in str) {
asc += str.charCodeAt(x).toString();
}
let freq = new Array(10).fill(0);
for (let x in asc) {
freq[asc.charCodeAt(x) - '0' .charCodeAt(0)]++;
}
for (let i = 0; i < 10; i++) {
document.write(`${freq[i]} `);
}
}
str = "GeeksForGeeks" ;
digitFreq(str);
</script>
|
Output
7 21 0 0 1 2 0 5 0 0
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...