FastPrepWord Frequency Counts in Alphabetical Order

Word Frequency Counts in Alphabetical Order

Abridge logoAbridge● EasyNEW GRADOA

Problem statement

You are given a list of words, words. The same word can appear more than once.

Count how many times each different word appears. Then return the counts as a list, ordered by their words in alphabetical order.

Alphabetical order compares two words letter by letter. If one word is the start of the other, such as ab and abc, the shorter word comes first.

Return only the counts, not the words.

Function

countWordFrequencies(words: String[]) → int[]

Examples

Example 1

words = ["pear","apple","pear","fig","apple","pear"]return = [2,1,3]

The distinct words in alphabetical order are apple, fig and pear. apple appears 2 times, fig 1 time and pear 3 times, so the answer is [2, 1, 3].

Example 2

words = ["b","ab","abc","ab","a"]return = [1,2,1,1]

In alphabetical order the distinct words are a, ab, abc and b. A word that is a prefix of another comes first, so ab comes before abc. Only ab appears twice.

Example 3

words = ["zoo"]return = [1]

There is one word, so the answer has one count.

Constraints

  • 1 <= words.length <= 2000
  • Each word has 1 to 10 lowercase English letters.

More Abridge problems

See Abridge hiring insights
Saved
public int[] countWordFrequencies(String[] words) {
    // Write your code here.
}
Checking account…