Phrase Covering the Most Words
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[]) → StringExamples
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
1and50non-empty words separated by one space. - Words contain printable ASCII characters other than spaces.
- The total number of words is at most
500.