1816. Truncate Sentence Easy

@problem@discussion
#Array#String



1/**
2 * [1816] Truncate Sentence
3 *
4 * 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).
5 * 
6 * 	For example, "Hello World", "HELLO", and "hello world hello world" are all sentences.
7 * 
8 * 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.
9 *  
10 * Example 1:
11 * 
12 * Input: s = "Hello how are you Contestant", k = 4
13 * Output: "Hello how are you"
14 * Explanation:
15 * The words in s are ["Hello", "how" "are", "you", "Contestant"].
16 * The first 4 words are ["Hello", "how", "are", "you"].
17 * Hence, you should return "Hello how are you".
18 * 
19 * Example 2:
20 * 
21 * Input: s = "What is the solution to this problem", k = 4
22 * Output: "What is the solution"
23 * Explanation:
24 * The words in s are ["What", "is" "the", "solution", "to", "this", "problem"].
25 * The first 4 words are ["What", "is", "the", "solution"].
26 * Hence, you should return "What is the solution".
27 * Example 3:
28 * 
29 * Input: s = "chopper is not a tanuki", k = 5
30 * Output: "chopper is not a tanuki"
31 * 
32 *  
33 * Constraints:
34 * 
35 * 	1 <= s.length <= 500
36 * 	k is in the range [1, the number of words in s].
37 * 	s consist of only lowercase and uppercase English letters and spaces.
38 * 	The words in s are separated by a single space.
39 * 	There are no leading or trailing spaces.
40 * 
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/truncate-sentence/
45// discuss: https://leetcode.com/problems/truncate-sentence/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50    pub fn truncate_sentence(s: String, k: i32) -> String {
51        String::new()
52    }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_1816() {
63    }
64}
65


Back
© 2025 bowen.ge All Rights Reserved.