Problem · Array
Top N Longest Sentences
Learn this problemProblem statement
You are given an array of ASCII strings sentences and an integer n. Rank the strings by decreasing character count. If two strings have the same length, the one that appeared earlier in the input ranks first.
Return the first n strings in that ranking.
Function
topLongestSentences(sentences: String[], n: int) → String[]Examples
Example 1
sentences = ["short","the longest one","medium text","tiny"]n = 2return = ["the longest one","medium text"]These are the two strings with the greatest character counts.
Example 2
sentences = ["bbb","aa","ccc","d"]n = 2return = ["bbb","ccc"]bbb and ccc tie at length 3, so their original order is preserved.
Constraints
1 <= sentences.length <= 1000001 <= n <= sentences.length0 <= sentences[i].length <= 1000- Every string contains only printable ASCII characters.
- The sum of all string lengths is at most
100000.