Skip to content

Commit cccef91

Browse files
Update 1816. Truncate Sentence.py
1 parent 9bec1dd commit cccef91

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
class Solution:
22
def truncateSentence(self, s: str, k: int) -> str:
3-
truncated_string = ''
3+
'''
4+
truncateSentence takes input string s and includes only the first k (int) words
5+
s: str (only lowercase and uppercase English letters and spaces, 1 <= s.length <= 500, words separated only by a single space, no leading/trailing spaces)
6+
k: int (range [1, number of words in s])
7+
'''
8+
truncated_string = '' #create empty string truncated_string
49
count = 0
5-
for char in s:
6-
if char == ' ':
10+
for char in s: #iterate thru characters in string
11+
if char == ' ': #if character is a space increase counter
712
count += 1
8-
if count == k:
13+
if count == k: #once count = k break the loop
914
break
10-
truncated_string += char
15+
truncated_string += char #add character to truncated_string
1116
return truncated_string

0 commit comments

Comments
 (0)