Skip to content

Commit 508dcd4

Browse files
authored
Merge pull request #49 from kobukuro/feat/truncate-sentence
Refactor TruncateSentence1816 with StringBuilder and add complexity a…
2 parents f39a287 + b2c6d3d commit 508dcd4

3 files changed

Lines changed: 100 additions & 18 deletions

File tree

src/TruncateSentence1816.java

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Tags: String
2+
package com.leetcode.easy;
3+
4+
public class TruncateSentence1816 {
5+
/**
6+
* Truncates the sentence to only contain the first k words.
7+
*
8+
* <p>A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
9+
* This method splits the input string and reconstructs it using only the first k words.</p>
10+
*
11+
* <h3>Algorithm:</h3>
12+
* <ol>
13+
* <li>Split the input string by spaces to get all words</li>
14+
* <li>Iterate through the first k words and append them to a StringBuilder</li>
15+
* <li>Return the concatenated result as a String</li>
16+
* </ol>
17+
*
18+
* <h3>Time Complexity: O(n)</h3>
19+
* <ul>
20+
* <li><b>split() operation:</b> O(n) - Scans the entire input string of length n once to find spaces</li>
21+
* <li><b>For loop:</b> O(k) iterations × O(1) per iteration = O(k)</li>
22+
* <li><b>StringBuilder appends:</b> O(m) total where m is total characters in first k words (amortized O(1) per append)</li>
23+
* <li><b>toString() call:</b> O(m) to create the final String</li>
24+
* <li><b>Total:</b> O(n) + O(k) + O(m) = O(n) since k ≤ n and m ≤ n</li>
25+
* </ul>
26+
*
27+
* <h3>Space Complexity: O(n)</h3>
28+
* <ul>
29+
* <li><b>words array:</b> O(n) - Stores all word references and substring objects from split</li>
30+
* <li><b>StringBuilder:</b> O(m) - Internal buffer stores up to m characters (first k words + spaces)</li>
31+
* <li><b>Result String:</b> O(m) - New String object created from StringBuilder</li>
32+
* <li><b>Total:</b> O(n) dominant, as the split array uses O(n) space regardless of k</li>
33+
* </ul>
34+
*
35+
* <h3>Example:</h3>
36+
* <pre>
37+
* Input: s = "Hello how are you", k = 2
38+
* Output: "Hello how"
39+
* Explanation: Returns only the first 2 words separated by a space
40+
* </pre>
41+
*
42+
* @param s the input sentence consisting of words separated by single spaces (length ≥ 1)
43+
* @param k the number of words to keep from the beginning (1 ≤ k ≤ number of words in s)
44+
* @return a new string containing only the first k words separated by spaces
45+
*/
46+
public String truncateSentence(String s, int k) {
47+
String[] words = s.split(" ");
48+
StringBuilder res = new StringBuilder();
49+
for (int i = 0; i < k; i++) {
50+
if (i > 0) res.append(" ");
51+
res.append(words[i]);
52+
}
53+
return res.toString();
54+
}
55+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.leetcode.easy;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class TruncateSentence1816Test {
9+
private TruncateSentence1816 solution;
10+
11+
@BeforeEach
12+
void setUp() {
13+
solution = new TruncateSentence1816();
14+
}
15+
16+
@Test
17+
void testTruncateSentence_Example1() {
18+
String s = "Hello how are you Contestant";
19+
int k = 4;
20+
21+
String result = solution.truncateSentence(s, k);
22+
23+
assertEquals("Hello how are you", result);
24+
}
25+
26+
@Test
27+
void testTruncateSentence_Example2() {
28+
String s = "What is the solution to this problem";
29+
int k = 4;
30+
31+
String result = solution.truncateSentence(s, k);
32+
33+
assertEquals("What is the solution", result);
34+
}
35+
36+
@Test
37+
void testTruncateSentence_Example3() {
38+
String s = "chopper is not a tanuki";
39+
int k = 5;
40+
41+
String result = solution.truncateSentence(s, k);
42+
43+
assertEquals("chopper is not a tanuki", result);
44+
}
45+
}

0 commit comments

Comments
 (0)