From b817f29ecaecbcbd9b1e4791bfbffa5253811ce2 Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Mon, 2 Mar 2026 09:01:29 +0800 Subject: [PATCH 1/2] Refactor TruncateSentence1816 with StringBuilder and add complexity analysis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Refactor from inefficient String concatenation to StringBuilder for O(n) performance - Add comprehensive JavaDoc with detailed time/space complexity analysis - Add JUnit 5 tests with proper setup and test cases - Move file to proper package structure (com.leetcode.easy) Changes: - Replace String.concat() loop (O(n²)) with StringBuilder (O(n)) - Document time complexity: O(n) where n is input string length - Document space complexity: O(n) for words array and result string - Add unit tests covering all LeetCode example cases --- src/TruncateSentence1816.java | 18 ------- .../leetcode/easy/TruncateSentence1816.java | 54 +++++++++++++++++++ .../easy/TruncateSentence1816Test.java | 45 ++++++++++++++++ 3 files changed, 99 insertions(+), 18 deletions(-) delete mode 100644 src/TruncateSentence1816.java create mode 100644 src/main/java/com/leetcode/easy/TruncateSentence1816.java create mode 100644 src/test/java/com/leetcode/easy/TruncateSentence1816Test.java diff --git a/src/TruncateSentence1816.java b/src/TruncateSentence1816.java deleted file mode 100644 index 9564d00..0000000 --- a/src/TruncateSentence1816.java +++ /dev/null @@ -1,18 +0,0 @@ -public class TruncateSentence1816 { - public String truncateSentence(String s, int k) { - String res = ""; - String[] words = s.split(" "); - for (int i = 0; i < k; i++) { - res = res.concat(words[i]); - res = res.concat(" "); - } - return res.substring(0, res.length() - 1); - } - - public static void main(String[] args) { - String s = "Hello how are you Contestant"; - int k = 4; - // Hello how are you - System.out.println(new TruncateSentence1816().truncateSentence(s, k)); - } -} diff --git a/src/main/java/com/leetcode/easy/TruncateSentence1816.java b/src/main/java/com/leetcode/easy/TruncateSentence1816.java new file mode 100644 index 0000000..9a356ab --- /dev/null +++ b/src/main/java/com/leetcode/easy/TruncateSentence1816.java @@ -0,0 +1,54 @@ +package com.leetcode.easy; + +public class TruncateSentence1816 { + /** + * Truncates the sentence to only contain the first k words. + * + *

A sentence is a list of words that are separated by a single space with no leading or trailing spaces. + * This method splits the input string and reconstructs it using only the first k words.

+ * + *

Algorithm:

+ *
    + *
  1. Split the input string by spaces to get all words
  2. + *
  3. Iterate through the first k words and append them to a StringBuilder
  4. + *
  5. Return the concatenated result as a String
  6. + *
+ * + *

Time Complexity: O(n)

+ * + * + *

Space Complexity: O(n)

+ * + * + *

Example:

+ *
+     * Input: s = "Hello how are you", k = 2
+     * Output: "Hello how"
+     * Explanation: Returns only the first 2 words separated by a space
+     * 
+ * + * @param s the input sentence consisting of words separated by single spaces (length ≥ 1) + * @param k the number of words to keep from the beginning (1 ≤ k ≤ number of words in s) + * @return a new string containing only the first k words separated by spaces + */ + public String truncateSentence(String s, int k) { + String[] words = s.split(" "); + StringBuilder res = new StringBuilder(); + for (int i = 0; i < k; i++) { + if (i > 0) res.append(" "); + res.append(words[i]); + } + return res.toString(); + } +} diff --git a/src/test/java/com/leetcode/easy/TruncateSentence1816Test.java b/src/test/java/com/leetcode/easy/TruncateSentence1816Test.java new file mode 100644 index 0000000..2c25f61 --- /dev/null +++ b/src/test/java/com/leetcode/easy/TruncateSentence1816Test.java @@ -0,0 +1,45 @@ +package com.leetcode.easy; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TruncateSentence1816Test { + private TruncateSentence1816 solution; + + @BeforeEach + void setUp() { + solution = new TruncateSentence1816(); + } + + @Test + void testTruncateSentence_Example1() { + String s = "Hello how are you Contestant"; + int k = 4; + + String result = solution.truncateSentence(s, k); + + assertEquals("Hello how are you", result); + } + + @Test + void testTruncateSentence_Example2() { + String s = "What is the solution to this problem"; + int k = 4; + + String result = solution.truncateSentence(s, k); + + assertEquals("What is the solution", result); + } + + @Test + void testTruncateSentence_Example3() { + String s = "chopper is not a tanuki"; + int k = 5; + + String result = solution.truncateSentence(s, k); + + assertEquals("chopper is not a tanuki", result); + } +} From b2c6d3dadb25c0639f040ca04b72dda3098fd86e Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Mon, 2 Mar 2026 09:11:14 +0800 Subject: [PATCH 2/2] Add category tag annotation to TruncateSentence1816. --- src/main/java/com/leetcode/easy/TruncateSentence1816.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/leetcode/easy/TruncateSentence1816.java b/src/main/java/com/leetcode/easy/TruncateSentence1816.java index 9a356ab..5719a89 100644 --- a/src/main/java/com/leetcode/easy/TruncateSentence1816.java +++ b/src/main/java/com/leetcode/easy/TruncateSentence1816.java @@ -1,3 +1,4 @@ +// Tags: String package com.leetcode.easy; public class TruncateSentence1816 {