1816. Truncate Sentence LeetCode Solution

In this guide, you will get 1816. Truncate Sentence LeetCode Solution with the best time and space complexity. The solution to Truncate Sentence problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Truncate Sentence solution in C++
  4. Truncate Sentence solution in Java
  5. Truncate Sentence solution in Python
  6. Additional Resources
1816. Truncate Sentence LeetCode Solution image

Problem Statement of Truncate Sentence

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.

Example 1:

Input: s = “Hello how are you Contestant”, k = 4
Output: “Hello how are you”
Explanation:
The words in s are [“Hello”, “how” “are”, “you”, “Contestant”].
The first 4 words are [“Hello”, “how”, “are”, “you”].
Hence, you should return “Hello how are you”.

Example 2:

Input: s = “What is the solution to this problem”, k = 4
Output: “What is the solution”
Explanation:
The words in s are [“What”, “is” “the”, “solution”, “to”, “this”, “problem”].
The first 4 words are [“What”, “is”, “the”, “solution”].
Hence, you should return “What is the solution”.
Example 3:

See also  1819. Number of Different Subsequences GCDs LeetCode Solution

Input: s = “chopper is not a tanuki”, k = 5
Output: “chopper is not a tanuki”

Constraints:

1 <= s.length <= 500
k is in the range [1, the number of words in s].
s consist of only lowercase and uppercase English letters and spaces.
The words in s are separated by a single space.
There are no leading or trailing spaces.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(k)

1816. Truncate Sentence LeetCode Solution in C++

class Solution {
 public:
  string truncateSentence(string s, int k) {
    for (int i = 0; i < s.length(); ++i)
      if (s[i] == ' ' && --k == 0)
        return s.substr(0, i);
    return s;
  }
};
/* code provided by PROGIEZ */

1816. Truncate Sentence LeetCode Solution in Java

class Solution {
  public String truncateSentence(String s, int k) {
    String[] words = s.split(" ");
    String[] truncated = new String[k];

    for (int i = 0; i < k; ++i)
      truncated[i] = words[i];

    return String.join(" ", truncated);
  }
}
// code provided by PROGIEZ

1816. Truncate Sentence LeetCode Solution in Python

class Solution:
  def truncateSentence(self, s: str, k: int) -> str:
    return ' '.join(s.split()[:k])
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.