Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions src/TruncateSentence1816.java

This file was deleted.

55 changes: 55 additions & 0 deletions src/main/java/com/leetcode/easy/TruncateSentence1816.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Tags: String
package com.leetcode.easy;

public class TruncateSentence1816 {
/**
* Truncates the sentence to only contain the first k words.
*
* <p>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.</p>
*
* <h3>Algorithm:</h3>
* <ol>
* <li>Split the input string by spaces to get all words</li>
* <li>Iterate through the first k words and append them to a StringBuilder</li>
* <li>Return the concatenated result as a String</li>
* </ol>
*
* <h3>Time Complexity: O(n)</h3>
* <ul>
* <li><b>split() operation:</b> O(n) - Scans the entire input string of length n once to find spaces</li>
* <li><b>For loop:</b> O(k) iterations × O(1) per iteration = O(k)</li>
* <li><b>StringBuilder appends:</b> O(m) total where m is total characters in first k words (amortized O(1) per append)</li>
* <li><b>toString() call:</b> O(m) to create the final String</li>
* <li><b>Total:</b> O(n) + O(k) + O(m) = O(n) since k ≤ n and m ≤ n</li>
* </ul>
*
* <h3>Space Complexity: O(n)</h3>
* <ul>
* <li><b>words array:</b> O(n) - Stores all word references and substring objects from split</li>
* <li><b>StringBuilder:</b> O(m) - Internal buffer stores up to m characters (first k words + spaces)</li>
* <li><b>Result String:</b> O(m) - New String object created from StringBuilder</li>
* <li><b>Total:</b> O(n) dominant, as the split array uses O(n) space regardless of k</li>
* </ul>
*
* <h3>Example:</h3>
* <pre>
* Input: s = "Hello how are you", k = 2
* Output: "Hello how"
* Explanation: Returns only the first 2 words separated by a space
* </pre>
*
* @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(" ");
Copy link

Copilot AI Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.split(" ") splits the entire sentence and allocates all words even though only the first k are used. If you want to avoid unnecessary work/allocations for long inputs, consider scanning for the k-th space (or using split(" ", k + 1) and only reading the first k tokens).

Suggested change
String[] words = s.split(" ");
String[] words = s.split(" ", k + 1);

Copilot uses AI. Check for mistakes.
StringBuilder res = new StringBuilder();
for (int i = 0; i < k; i++) {
if (i > 0) res.append(" ");
res.append(words[i]);
}
return res.toString();
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/leetcode/easy/TruncateSentence1816Test.java
Original file line number Diff line number Diff line change
@@ -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);
}
}