Skip to content

Commit f458560

Browse files
authored
Create truncate-sentence.py
1 parent 8dac922 commit f458560

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Python/truncate-sentence.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def truncateSentence(self, s, k):
6+
"""
7+
:type s: str
8+
:type k: int
9+
:rtype: str
10+
"""
11+
for i in xrange(len(s)):
12+
if s[i] == ' ':
13+
k -= 1
14+
if not k:
15+
return s[:i]
16+
return s

0 commit comments

Comments
 (0)