按字母顺序打印每个字符的频率
给定一个字符串str ,任务是按字母顺序打印str中每个字符的频率。
例子:
Input: str = “aabccccddd”
Output: a2b1c4d3
Since it is already in alphabetical order, the frequency
of the characters is returned for each character.
Input: str = “geeksforgeeks”
Output: e4f1g2k2o1r1s2
方法:
- 创建一个 Map 来存储给定字符串中每个字符的频率。
- 遍历字符串并检查该字符是否存在于地图中。
- 如果字符不存在,则以 1 作为初始值将其插入到映射中,否则将其频率增加 1。
- 最后,按字母顺序打印每个字符的频率。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
const int MAX = 26;
// Function to print the frequency
// of each of the characters of
// s in alphabetical order
void compressString(string s, int n)
{
// To store the frequency
// of the characters
int freq[MAX] = { 0 };
// Update the frequency array
for (int i = 0; i < n; i++) {
freq[s[i] - 'a']++;
}
// Print the frequency in alphatecial order
for (int i = 0; i < MAX; i++) {
// If the current alphabet doesn't
// appear in the string
if (freq[i] == 0)
continue;
cout << (char)(i + 'a') << freq[i];
}
}
// Driver code
int main()
{
string s = "geeksforgeeks";
int n = s.length();
compressString(s, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MAX = 26;
// Function to print the frequency
// of each of the characters of
// s in alphabetical order
static void compressString(String s, int n)
{
// To store the frequency
// of the characters
int freq[] = new int[MAX] ;
// Update the frequency array
for (int i = 0; i < n; i++)
{
freq[s.charAt(i) - 'a']++;
}
// Print the frequency in alphatecial order
for (int i = 0; i < MAX; i++)
{
// If the current alphabet doesn't
// appear in the string
if (freq[i] == 0)
continue;
System.out.print((char)(i + 'a') +""+ freq[i]);
}
}
// Driver code
public static void main (String[] args)
{
String s = "geeksforgeeks";
int n = s.length();
compressString(s, n);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach
MAX = 26;
# Function to print the frequency
# of each of the characters of
# s in alphabetical order
def compressString(s, n) :
# To store the frequency
# of the characters
freq = [ 0 ] * MAX;
# Update the frequency array
for i in range(n) :
freq[ord(s[i]) - ord('a')] += 1;
# Print the frequency in alphatecial order
for i in range(MAX) :
# If the current alphabet doesn't
# appear in the string
if (freq[i] == 0) :
continue;
print((chr)(i + ord('a')),freq[i],end = " ");
# Driver code
if __name__ == "__main__" :
s = "geeksforgeeks";
n = len(s);
compressString(s, n);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int MAX = 26;
// Function to print the frequency
// of each of the characters of
// s in alphabetical order
static void compressString(string s, int n)
{
// To store the frequency
// of the characters
int []freq = new int[MAX] ;
// Update the frequency array
for (int i = 0; i < n; i++)
{
freq[s[i] - 'a']++;
}
// Print the frequency in alphatecial order
for (int i = 0; i < MAX; i++)
{
// If the current alphabet doesn't
// appear in the string
if (freq[i] == 0)
continue;
Console.Write((char)(i + 'a') +""+ freq[i]);
}
}
// Driver code
public static void Main()
{
string s = "geeksforgeeks";
int n = s.Length;
compressString(s, n);
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
e4f1g2k2o1r1s2
时间复杂度: O(n)
辅助空间: O(1)