原文:
给定一个字符串 str ,任务是按字母顺序打印 str 的每个字符的频率。 例:
输入: str = "aabccccddd" 输出: a2b1c4d3 由于已经按字母顺序排列,所以返回每个字符的字符频率 。 输入:str = " geeksforgeeks " 输出: e4f1g2k2o1r1s2
进场:
- 创建一个映射来存储给定字符串中每个字符的频率。
- 遍历字符串并检查字符是否出现在地图中。
- 如果字符不存在,将它以 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
python 3
# 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
java 描述语言
output:
e4f1g2k2o1r1s2
时间复杂度:o(n) t3】辅助空间: o(1)
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处