-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor TruncateSentence1816 with StringBuilder and add complexity a… #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| 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(" "); | ||||||
|
||||||
| String[] words = s.split(" "); | |
| String[] words = s.split(" ", k + 1); |
| 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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.