Skip to content

Commit f48cd08

Browse files
authored
Added Truncate Sentence.java
1 parent 9491375 commit f48cd08

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

Easy/Truncate Sentence.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public String truncateSentence(String s, int k) {
3+
int idx = 0;
4+
while (idx < s.length() && k > 0) {
5+
if (s.charAt(idx) == ' ') {
6+
k--;
7+
}
8+
idx++;
9+
}
10+
return s.substring(0, (idx == s.length() ? idx : idx - 1));
11+
}
12+
}

0 commit comments

Comments
 (0)