FastPrepTop N Longest Sentences
Problem · Array

Top N Longest Sentences

Learn this problem
EasyDeloitte logoDeloitteNEW GRADOA

Problem 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 <= 100000
  • 1 <= n <= sentences.length
  • 0 <= sentences[i].length <= 1000
  • Every string contains only printable ASCII characters.
  • The sum of all string lengths is at most 100000.

More Deloitte problems

drafts saved locally
public String[] topLongestSentences(String[] sentences, int n) {
    // Write your solution here.
}
sentences["short","the longest one","medium text","tiny"]
n2
expected["the longest one", "medium text"]
checking account