Ordered Symbol Tally by First Occurrence
StringsAuto-graded · 19 test casesLast updated
Count how often each character occurs in a string and report the tallies in the order the characters first appear, treating every character as distinct and significant.
Ordered Symbol Tally
You are given a string text. Produce a tally of how many times each distinct character occurs in it.
Rules
- Every character counts, including spaces, digits, punctuation, and any other Unicode character. Nothing is filtered out.
- Matching is case-sensitive:
'A'and'a'are different characters. - The tally must be ordered by first occurrence: a character appears in the result at the position where it first shows up in
text, not sorted alphabetically or by count. - Each entry in the result is a two-element list
[character, count], wherecharacteris a one-character string andcountis an integer. The function returns a list of these entries. - If
textis empty, return an empty list.
Task
Implement the function so that it returns the list of [character, count] pairs described above.
Constraints
0 <= len(text) <= 10^6textmay contain any Unicode character. Characters are the code points Python produces when iterating over the string.- Aim for
O(n)time, wheren = len(text).
Examples
Example 1
Input: ordered_symbol_tally('hello')
Output: [['h', 1], ['e', 1], ['l', 2], ['o', 1]]
Example 2
Input: ordered_symbol_tally('AaBb')
Output: [['A', 1], ['a', 1], ['B', 1], ['b', 1]]
Example 3
Input: ordered_symbol_tally('a a!')
Output: [['a', 2], [' ', 1], ['!', 1]]
Example 4
Input: ordered_symbol_tally('')
Output: []
Hints
Hint 1
You need both a count per character and a record of when each character was first seen. Think about a single structure that gives you both.
Hint 2
A Python dict remembers insertion order. If you only add a key the first time you see its character, later updates will not change its position.
Hint 3
Avoid calling `text.count(ch)` per distinct character, since that is quadratic in the worst case. Do one pass and build the output as lists, `[ch, count]`, not tuples.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through the string once while maintaining a dictionary that maps each character to its running count. Python dictionaries keep keys in the order they were first inserted. A character is inserted only when it is first seen, and later occurrences just update its value in place. So after the pass, iterating over the dictionary yields the characters in order of first appearance, each paired with its total count. Convert each pair to a two-element list for the output.
Why it is correct
- Exact counts: every character is visited once and increments its own entry, so the final value equals the number of occurrences.
- First-appearance order: a key's position is fixed at insertion and never changes when its value is updated.
- Case sensitivity and full coverage: different code points are different keys, and no characters are skipped, so
'A'and'a'get separate entries and spaces or punctuation are counted like any other character. - Empty input: the loop does not run, the dictionary is empty, and the result is
[].
Complexity
- Time:
O(n), with one average-constant-time dictionary operation per character, plusO(k)to build the output, wherek <= nis the number of distinct characters. - Space:
O(k)for the dictionary and the result.
Reference solution
from typing import List
def ordered_symbol_tally(text: str) -> List[List[object]]:
counts = {}
for ch in text:
counts[ch] = counts.get(ch, 0) + 1
return [[ch, count] for ch, count in counts.items()]