Description
A sentence is a list of words separated by a single space. Given a sentence s and an integer k, return the sentence truncated to contain only the first k words.
Examples
Input:
s = "Hello how are you Contestant", k = 4Output:
"Hello how are you"Explanation:
First 4 words.
Input:
s = "Programming is fun and rewarding when you practice regularly", k = 1Output:
ProgrammingExplanation:
When k=1, only the first word from the sentence is returned.
Input:
s = "The quick brown fox jumps", k = 5Output:
The quick brown fox jumpsExplanation:
When k equals the total number of words in the sentence, the entire sentence is returned unchanged.
Constraints
- •
1 ≤ s.length ≤ 500