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..5719a89 --- /dev/null +++ b/src/main/java/com/leetcode/easy/TruncateSentence1816.java @@ -0,0 +1,55 @@ +// Tags: String +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); + } +}