Problem overview
The method LetterCount receives a string word and returns a Dictionary<char, int>. Each letter of the word is a key, and its value is the number of times that letter appears. A letter that doesn't occur has no entry, and the order of the entries doesn't matter.
For "banana" the result has three entries: b is 1, a is 3 and n is 2. For "hello" it is h 1, e 1, l 2, o 1, and for "a" it is just a 1.
Approach: a counting dictionary
A dictionary maps keys to values, and here the key is a letter and the value is a tally. Go through the letters once. For each letter, either it has been seen before, and its tally goes up by one, or it is new, and its tally starts at 1.
The two cases are needed because of how a Dictionary behaves. Reading a key that isn't there, as in counts[letter] += 1 on a new letter, throws a KeyNotFoundException. Writing to a key that isn't there is fine: counts[letter] = 1 adds the entry. So the code checks first with ContainsKey.
Step by step
- Create an empty
Dictionary<char, int>namedcounts. The key type ischarand the value type isint. - Write a
foreachloop with acharnamedletteroverword. Aforeachover a string visits its characters one by one, like the items of an array. - If
counts.ContainsKey(letter)is true, add 1 withcounts[letter] += 1. The letter was seen before, so it already has a tally. - Otherwise set
counts[letter]to 1. This is the first time the letter shows up. - Return
countsafter the loop.
The solution in C#
class Solution {
public Dictionary<char, int> LetterCount(string word) {
Dictionary<char, int> counts = new Dictionary<char, int>();
foreach (char letter in word) {
if (counts.ContainsKey(letter)) {
counts[letter] += 1;
} else {
counts[letter] = 1;
}
}
return counts;
}
}
On "banana" the loop meets b (new, tally 1), a (new, 1), n (new, 1), then a again (2), n again (2) and a once more (3). The returned dictionary holds b: 1, a: 3, n: 2.
Details worth knowing:
- Characters are written with single quotes in C# (
'a'), strings with double quotes ("a"). The keys here arecharvalues, so a test for the letteralooks up'a'. counts[letter] += 1reads the current value, adds 1 and stores it back, so it is only safe when the key exists.- When printing a dictionary, the entries typically come out in the order they were added, but the order isn't something to rely on, and the lesson's tests don't depend on it.
Complexity
The loop runs once per letter, and ContainsKey and the indexer take constant time on average, so the time is O(n) for a word of n letters. The dictionary holds at most one entry per distinct letter, so the space is O(k) for k distinct letters, at most 26 here.
Edge cases and common mistakes
- Skipping the check. Writing
counts[letter] += 1alone throws aKeyNotFoundExceptionon the first letter. - Setting instead of adding. Writing
counts[letter] = 1in every case leaves every tally at 1. - Using the wrong key type. A
Dictionary<string, int>would needletter.ToString(). The method promisesDictionary<char, int>. - One letter.
"a"gives a single entry with the value 1.
