One minute
Leetcode 1816
Since the solution wants us to return the first k
words
- We can split the words
- Then we can return the the first
k
words joined together seperated by spaces.
The Solution
func truncateSentence(s string, k int) string {
return strings.Join(strings.Split(s, " ")[:k], " ")
}
Edited Solution for readability
func truncateSentence(s string, k int) string {
arr := strings.Split(s, " ")
return strings.Join(arr[:k], " ")
}
Read other posts