通过连接给定字符串的 ASCII 值查找每个数字 0-9 的频率
给定字符串str,任务是在通过连接给定字符串str的每个字符的 ASCII 值创建的字符串中找到所有数字(0-9)的频率。
例子:
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
方法:给定的问题是一个基于实现的问题,可以通过以下给定的步骤来解决:
- 创建一个字符串asc ,它存储每个字符的 ASCII 值。
- 遍历给定的字符串str ,找到每个字符的 ASCII 值并使用 to 字符串内置函数将其附加到asc中。
- 遍历字符串asc并更新存储在频率数组中的当前数字的频率。
- 打印每个数字的频率。
下面是上述方法的实现:
C++
// C++ program of the above approach
#include
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
// 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.
C#
// 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.
Python3
# 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
Javascript
输出
7 21 0 0 1 2 0 5 0 0
时间复杂度: O(N)
辅助空间: O(N)