LeetCode solutions

1944. Truncate Sentence

My accepted Python solution to LeetCode problem 1944, Truncate Sentence, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.5MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.5MB, accepted 2025-12-29.

python
class Solution:
    def truncateSentence(self, s: str, k: int) -> str:
        # Split and take first k words
        # Time: O(n), Space: O(n)
        return ' '.join(s.split()[:k])

Source