#1816
Easy Algorithms Truncate sentence
Array String
86.7% acceptance
Feb 25, 2026
1224
34
A sentence is a list of words that are separated by a single space with no leading or trailing spaces. Each of the words consists of only uppercase and lowercase English letters (no punctuation).
For example, "Hello World", "HELLO", and "hello world hello world" are all sentences.
You are given a sentence s and an integer k. You want to truncate s such that it contains only the first k words. Return s after truncating it.
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn truncate_sentence(s: String, k: i32) -> String {
s.split_whitespace()
.take(k as usize)
.collect::<Vec<&str>>()
.join(" ")
}
}