Alphabetical Character Frequency

Easy
0/40
1 upvote
Asked in companies
HSBCAmdocs

Problem statement

You are given a string s. Your task is to calculate the frequency of each character present in the string and print the results.

The output must follow these specific rules:

1) The character counting is case-sensitive (e.g., 'a' and 'A' are considered different characters). 2) Any spaces (' ') in the input string must be ignored. 3) The final frequencies must be printed in alphabetical order of the characters.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of input contains a single string s.


Output Format:
For each unique character (excluding spaces) found in the string, print a new line in the format: char: count

The lines must be sorted alphabetically by the character.


Note:
Alphabetical order refers to the standard character sort order (also known as ASCIIbetical order), where uppercase letters come before lowercase letters, and numbers/symbols have their own standard positions.
Sample Input 1:
Hello World


Sample Output 1:
H: 1
W: 1
d: 1
e: 1
l: 3
o: 2
r: 1


Explanation for Sample 1:
After ignoring the space, the characters are H, e, l, l, o, W, o, r, l, d.
The frequencies are: H(1), e(1), l(3), o(2), W(1), r(1), d(1).
Printed in alphabetical (ASCII) order, this gives the specified output.


Sample Input 2:
Programming is fun


Sample Output 2:
P: 1
a: 1
f: 1
g: 2
i: 2
m: 2
n: 2
o: 1
r: 2
s: 1
u: 1


Explanation for Sample 2:
The character 'r' appears twice, 'g' appears twice, 'm' appears twice, 'i' appears twice, and 'n' appears twice. All other characters appear once. The output is sorted by the character.


Expected Time Complexity:
The expected time complexity is O(N log K), where N is the length of the string and K is the number of unique characters.


Constraints:
0 <= length of s <= 10^5
The string consists of standard ASCII characters.

Time limit: 1 sec
Approaches (1)
Alphabetical Character Frequency
Time Complexity

The expected time complexity is O(N log K), where N is the length of the string and K is the number of unique characters.

Space Complexity
Code Solution
(100% EXP penalty)
Alphabetical Character Frequency
Full screen
Console