Skills101
← All coding problems

Character Frequency in Order

easyPlacement Coding+10 XPTime limit 2s

Count how many times each character appears in a word. Print the characters in the order of their first appearance, each followed by its count. For programming the answer is p1 r2 o1 g2 a1 m2 i1 n1.

Input: one line with a word of lowercase letters.

Output: for each distinct character, the character followed immediately by its count; separate the pairs with spaces.

Constraints: 1 ≤ length ≤ 10⁵.

Think about it: an array of 26 counts finds the totals in one pass. A second pass over the word prints each character the first time you meet it (mark it as printed).

Sample input 1

programming

Sample output 1

p1 r2 o1 g2 a1 m2 i1 n1

Sample input 2

aaaa

Sample output 2

a4

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.