-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_1816.java
More file actions
27 lines (25 loc) · 744 Bytes
/
_1816.java
File metadata and controls
27 lines (25 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.github.aditya;
public class _1816 {
public static class Solution {
public String truncateSentence(String s, int k) {
int counter = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ')
counter++;
if (counter == k)
return s.substring(0, i);
}
return s;
}
}
public static class Solution_1 {
public String truncateSentence(String s, int k) {
String[] strArr = s.split(" ");
String str = strArr[0];
for (int i = 1; i < k; i++) {
str = str + " " + strArr[i];
}
return str;
}
}
}