Count how often each word occurs
Coding problemEasy-MediumAsked in: Infosys
Given a sentence, count how many times each word occurs. Words are separated by one or more spaces, and the sentence may start or end with spaces.
Compare words without regard to case: "Test" and "test" are the same word. Return one entry per distinct word, written in lower case as the word, an equals sign and the count (such as "test=2"), in the order the words first appear.
Examples
Example 1
Input: sentence = "the cat and the hat"
Output: ["the=2","cat=1","and=1","hat=1"]
Example 2
Input: sentence = "Hello hello HELLO"
Output: ["hello=3"]
Example 3
Input: sentence = " "
Output: []
Constraints
- The length of sentence is between 0 and 100,000
- Words hold English letters and digits; only the space character separates them
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.