Maintained by Vikas Dulgunde, 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.
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.
Visible test cases
Splitting the sentence on the space character turns it into a list of individual words you can inspect one at a time.
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".
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.
Break the sentence into its words, then compare each word against the target and tally the exact matches.
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.