Word Frequency Count
Maintained by Vikas D, software engineer
You are handed a sentence made of lowercase words, each separated from the next by exactly one space. You are also given a single target word.
Your job is to report how many of the sentence's words are an exact match for the target. Matching is whole-word, so a target of "cat" should not be triggered by the word "category" or "scat".
Because the separator is always a single space and the text is already lowercase, you can lean on a plain split and a straight equality check. Return an integer count, which can be zero when the word never appears.
Examples
Splitting on spaces gives ten words, and "the" sits at positions 1, 7, and 10, so the count is 3.
Neither "hello" nor "world" equals "hi", and partial overlap does not count, so the answer is 0.
Every one of the four tokens is exactly "a", so each contributes to the count.
Constraints
- 1 <= sentence.length <= 10^4
- sentence holds only lowercase English letters and single spaces between words
- Words are separated by exactly one space, with no leading or trailing spaces
- 1 <= word.length <= 100, word is lowercase letters only
Visible test cases
- countWord("the quick brown fox jumps over the lazy dog the", "the") == 3
- countWord("hello world", "hi") == 0
- countWord("apple", "apple") == 1
- countWord("cat bat cat cat hat", "cat") == 3
- countWord("one two three", "four") == 0
- countWord("a a a a", "a") == 4
Hints
Hint 1
Splitting the sentence on the space character turns it into a list of individual words you can inspect one at a time.
Hint 2
Walk the list and increase a running total every time a word is exactly equal to the target. Equality on the whole token is what keeps substrings like "category" from matching "cat".
Hint 3
You do not need to store every word or build a map. A single counter that you raise on each exact match is enough for one fixed target word.
Approaches
Split and scan
Time O(n) where n is the length of the sentence, since splitting and the single pass each touch every character once. · Space O(n) for the array of words produced by the split.Break the sentence into its words, then compare each word against the target and tally the exact matches.
- Split the sentence on the single space to get an array of words.
- Initialize a counter to zero.
- Loop over the words, and for each one that equals the target word, add one to the counter.
- Return the counter.
Single-pass scan without a word array
Time O(n) since each character is visited once and each comparison is bounded by the target word length. · Space O(1) extra beyond the input and the target, because no separate word list is stored.Avoid materializing the full word list by walking the characters once, detecting word boundaries on the fly, and comparing each finished word against the target.
- Track the start index of the current word as you move through the sentence character by character.
- When you hit a space or the end of the string, you have one complete word spanning from the start index to the current position.
- Compare that slice against the target word and bump the counter when they match, then reset the start index past the space.
- Return the counter after the final word is checked.
Common mistakes
- Using a substring or 'contains' check instead of whole-word equality, which wrongly counts "the" inside "theme" or "other".
- Splitting on a regex for general whitespace and then forgetting the empty-string token that appears at the edges of some inputs; here the guaranteed single-space format means a plain space split is safe.
- Returning early after the first match instead of continuing to count every occurrence.
- Assuming the word always exists and not handling the zero-occurrence case, which is a valid result.
Follow-up variations
- What changes if the matching should be case-insensitive, so "The" and "the" count as the same word?
- How would you count occurrences for many target words at once in a single pass over the sentence?
- How would you adapt this if words could be separated by varied whitespace, punctuation, or multiple spaces?
- Could you return a full frequency map of every distinct word instead of the count for just one word?