Tap here to switch tabs
Problem
Submissions

Problem: Truncate Sentence

med
30 min
Try to solve the Truncate Sentence problem.

Statement

A sentence is defined as a sequence of words separated by single spaces, without any leading or trailing spaces.

Given a sentence s and an integer k, truncate s such that it contains only its first k words. Return the resulting string.

Constraints:

  • 1≤s.length≤5001 \le s. length \le 500

  • k is in the range [1,the number of words in s][1, the \space number \space of \space words \space in \space s].

  • s consists 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.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Truncate Sentence

med
30 min
Try to solve the Truncate Sentence problem.

Statement

A sentence is defined as a sequence of words separated by single spaces, without any leading or trailing spaces.

Given a sentence s and an integer k, truncate s such that it contains only its first k words. Return the resulting string.

Constraints:

  • 1≤s.length≤5001 \le s. length \le 500

  • k is in the range [1,the number of words in s][1, the \space number \space of \space words \space in \space s].

  • s consists 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.