FastPrepPhrase Covering the Most Words

Phrase Covering the Most Words

Zip logoZip● MediumFULLTIMENEW GRADPHONE SCREEN
Learn

Problem statement

Given an array of space-separated sentences, choose one non-empty contiguous phrase that appears in one or more sentences.

A phrase's coverage score is:

number of words in the phrase × number of distinct sentences containing the phrase.

Return the phrase with the greatest coverage score. If several phrases tie, prefer the phrase with more words. If a tie remains, return the lexicographically smallest phrase.

Function

mostSharedPhrase(sentences: String[]) → String

Examples

Example 1

sentences = ["I feel very good","I feel very bad","hello","hello world"]return = "I feel very"

The phrase has three words and appears in two sentences, so its score is 3 × 2 = 6. No other phrase scores more.

Example 2

sentences = ["a b","a c"]return = "a b"

a scores 1 × 2 = 2. Each two-word sentence also scores 2 × 1 = 2, so the longer phrase wins; a b is lexicographically smaller than a c.

Example 3

sentences = ["one two three"]return = "one two three"

With one sentence, the entire sentence has the maximum possible word count and score.

Constraints

  • 1 <= sentences.length <= 100.
  • Each sentence contains between 1 and 50 non-empty words separated by one space.
  • Words contain printable ASCII characters other than spaces.
  • The total number of words is at most 500.

More Zip problems

See Zip hiring insights
public String mostSharedPhrase(String[] sentences) {
    // Write your code here.
}
sentences["I feel very good","I feel very bad","hello","hello world"]
expected"I feel very"
Checking account…