Difficulty: Easy | Acceptance: 86.80% | Paid: No Topics: Array, String
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.
Given a string s representing a sentence and an integer k, return the first k words of the sentence.
- Examples
- Constraints
- Split and Join
- Iterative Character Processing
- Two Pointers
Examples
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
Input:
s = "chopper is not a tanuki", k = 5
Output:
"chopper is not a tanuki"
Constraints
1 <= s.length <= 500
s consists of only English letters and spaces ' '.
s contains no leading or trailing spaces.
All the words in s are separated by a single space.
1 <= k <= s.length
Split and Join
Intuition Split the sentence into words by spaces, take the first k words, and join them back with spaces.
Steps
- Split the string by space character
- Slice the array to get first k elements
- Join the sliced array with spaces
class Solution:
def truncateSentence(self, s: str, k: int) -> str:
words = s.split(' ')
return ' '.join(words[:k])Complexity
- Time: O(n) where n is the length of the string
- Space: O(n) for storing the split words array
- Notes: Clean and readable, uses built-in string methods
Iterative Character Processing
Intuition Iterate through each character, build words one by one, and stop after collecting k words.
Steps
- Track word count and current word being built
- When a space is encountered, increment word count and add word to result
- Stop when k words are collected
- Handle the last word if it’s the k-th word
class Solution:
def truncateSentence(self, s: str, k: int) -> str:
word_count = 0
result = []
current_word = []
for char in s:
if char == ' ':
word_count += 1
result.append(''.join(current_word))
current_word = []
if word_count == k:
break
else:
current_word.append(char)
if word_count < k and current_word:
result.append(''.join(current_word))
return ' '.join(result)Complexity
- Time: O(n) where n is the length of the string
- Space: O(n) for storing the result string
- Notes: More manual control, avoids creating intermediate arrays
Two Pointers
Intuition Find the position of the k-th space and return the substring up to that position.
Steps
- Iterate through the string counting spaces
- When the k-th space is found, return substring from start to that position
- If k spaces are never found, return the entire string
class Solution:
def truncateSentence(self, s: str, k: int) -> str:
word_count = 0
for i in range(len(s)):
if s[i] == ' ':
word_count += 1
if word_count == k:
return s[:i]
return sComplexity
- Time: O(n) where n is the length of the string
- Space: O(n) for the result substring
- Notes: Most space-efficient as it doesn’t store intermediate arrays