Problem · String

Lexicographic Word Frequencies

Learn this problem
EasyUpstart logoUpstartFULLTIMEOA

Problem statement

Given an array of words words, count each distinct word and return the counts in lexicographic order.

Words contain lowercase English letters and comparisons are exact. Return a two-dimensional string array where every row is [word, decimalCount]. Return an empty array when the input is empty.

Function

wordFrequencies(words: String[]) → String[][]

Examples

Example 1

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

apple appears three times, banana once, and pear twice. The rows are ordered by word.

Example 2

words = []return = []

An empty input has no frequency rows.

Example 3

words = ["zoo","ant","zoo","bee","ant"]return = [["ant","2"],["bee","1"],["zoo","2"]]

The distinct words are returned in ascending lexicographic order with decimal counts.

Constraints

  • 0 <= words.length <= 200000
  • Every word has length from 1 through 40.
  • Every word contains only lowercase English letters.
  • The total number of input characters is at most 10^6.

More Upstart problems

drafts saved locally
public String[][] wordFrequencies(String[] words) {
    // Write your code here.
}
words["pear","apple","pear","banana","apple","apple"]
expected[["apple", "3", "banana", "1", "pear", "2"]]
checking account