Top Counted Word Records
Problem statement
Each string in records contains a word and its count, separated by one comma. Return the highest-count records whose word length is at least minWordLength, with at most wordLimit results.
For this exercise, assume surrounding ASCII spaces in each field are ignored. Every input occurrence is a separate record: duplicate words and identical records are preserved, and counts are neither added nor overwritten.
For this exercise, assume records sort by descending count, then case-sensitive ASCII word order, then earlier input index. Return each selected record as its trimmed word, a comma and one space, and its count in canonical decimal form. Empty input, no eligible records or a zero limit returns an empty array.
Function
getTopWords(records: String[], wordLimit: int, minWordLength: int) → String[]Examples
Example 1
records = ["abc, 500","sadhasjhkgdsak, 230239203","fsgdfssd, 78","sss, 56","ss, 56","sss, 5678 ","sssdsds, 56","ssssdsd, 56"]wordLimit = 3minWordLength = 3return = ["sadhasjhkgdsak, 230239203","sss, 5678","abc, 500"]The three highest eligible records are sadhasjhkgdsak with 230239203, sss with 5678, and abc with 500. The three-letter abc meets the minimum length.
Example 2
records = ["cat, 7","ant, 7","dog, 8","a, 100"]wordLimit = 2minWordLength = 3return = ["dog, 8","ant, 7"]The short a record is excluded. Dog ranks first by count; ant wins the seven-count cutoff tie by ASCII word order.
Constraints
- For this exercise, assume
0 <= records.length <= 500,0 <= wordLimit <= 500and0 <= minWordLength <= 31. - Every record has exactly one comma. For this exercise, assume each trimmed word has
1through30ASCII letters, and each count has1through10decimal digits with value from0through1000000000. - For this exercise, assume each field may have up to two ASCII spaces before and after it. Leading zeros in a count are accepted; word comparison is case-sensitive.