Return the Sentence with the Most Words
Updated
Problem statement
Split text at each comma ,, period ., question mark ? or exclamation mark !. The final part is also a sentence, even without a trailing delimiter.
A word is a nonempty run of English letters. One or more spaces separate words within a sentence.
Return the sentence with the most words. Break ties by choosing the earliest sentence. Remove surrounding spaces from the returned sentence, preserve its internal spaces, and exclude its delimiter. If there are no words, return the empty string.
Function
sentenceWithMostWords(text: String) → StringExamples
Example 1
text = "Read,Try this now!Keep going."return = "Try this now"The nonempty parts have 1, 3 and 2 words. Return the three-word sentence without its exclamation mark.
Example 2
text = "One two.Three four"return = "One two"Both sentences have two words, so return the earlier one.
Example 3
text = " , . ? ! "return = ""The parts contain only spaces, so there is no sentence with a word.
Constraints
- 0 <= text.length <= 100000.
- text contains only English letters, ordinary spaces, commas, periods, question marks and exclamation marks.
More LSEG Online Assessment questions
- Count Times Using at Most Two DigitsOA · Seen Jul 2025
- Maximum Sum of Exactly Three Non-Adjacent ElementsOA · Seen Jul 2025
- Maximum Word Count in a SentenceOA · Seen Jul 2025
- Minimum Digit Swaps for the Smallest DifferenceOA · Seen Jul 2025
- Smallest String After at Most One DeletionOA · Seen Jul 2025
- Delete One Digit 5 to Maximize the NumberOA · Seen Feb 2025
- Three-Part Partition with Minimum Largest RangeOA · Seen Feb 2025
- Transform a String with at Most One OperationOA · Seen Feb 2025
- Price Equity Options and ForwardsOA · Seen Dec 2024
- Maximum Sum of Non-Adjacent ElementsOA · Seen Sep 2024
Similar questions at other companies
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- First Non-Repeating CharacterAmazon · OA · ONSITE INTERVIEW
- Maximum Ones After K OperationsGoldman Sachs · OAGoldman Sachs interview questions
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- Count Palindrome Addition ChoicesThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Count Palindrome Addition ChoicesHudson River Trading · OAHudson River Trading interview questions
- Count Phrases Formed by Anagram ReplacementWells Fargo · OAWells Fargo interview questions
- Count Substrings With Identical CharactersVirtu Financial · OAVirtu Financial interview questions