183. Truncate Sentence

EasyStringString

A sentence is a list of words that are separated by a single space with no leading or trailing spaces.

Given a sentence s and an integer k, return the sentence formed by the first k words of s.

Input: A comma-separated pair: sentence s and integer k.

Output: A string — the first k words of s joined by single spaces.

Examples

Example 1
Input: Hello how are you Contestant,4
Output: Hello how are you
Explanation: First 4 words: 'Hello','how','are','you'.
Example 2
Input: What is the solution to this problem,4
Output: What is the solution
Explanation: First 4 words of the sentence.
Example 3
Input: chopper is not a tanuki,5
Output: chopper is not a tanuki
Explanation: k equals total word count — return full sentence.

Constraints

Asked by

BloombergMeta
Solve this problem in the editor →