From 5931fd38217cbfc2de8b1129c1ffa2eba203620c Mon Sep 17 00:00:00 2001 From: Peter Chen Date: Tue, 10 Mar 2026 09:00:41 +0800 Subject: [PATCH] Add solution for "Truncate Sentence" with unit tests. --- String/1816. Truncate Sentence.py | 9 --------- .../easy/truncate_sentence_1816/__init__.py | 0 .../easy/truncate_sentence_1816/solution.py | 20 +++++++++++++++++++ .../truncate_sentence_1816/test_solution.py | 15 ++++++++++++++ 4 files changed, 35 insertions(+), 9 deletions(-) delete mode 100644 String/1816. Truncate Sentence.py create mode 100644 problems/easy/truncate_sentence_1816/__init__.py create mode 100644 problems/easy/truncate_sentence_1816/solution.py create mode 100644 problems/easy/truncate_sentence_1816/test_solution.py diff --git a/String/1816. Truncate Sentence.py b/String/1816. Truncate Sentence.py deleted file mode 100644 index 34cec12..0000000 --- a/String/1816. Truncate Sentence.py +++ /dev/null @@ -1,9 +0,0 @@ -# String, Array -class Solution: - def truncateSentence(self, s: str, k: int) -> str: - words = s.split(' ') - res = '' - for i in range(k): - res += words[i] - res += ' ' - return res[:-1] diff --git a/problems/easy/truncate_sentence_1816/__init__.py b/problems/easy/truncate_sentence_1816/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/easy/truncate_sentence_1816/solution.py b/problems/easy/truncate_sentence_1816/solution.py new file mode 100644 index 0000000..2090642 --- /dev/null +++ b/problems/easy/truncate_sentence_1816/solution.py @@ -0,0 +1,20 @@ +# Tags: String + + +class Solution: + def truncate_sentence(self, s: str, k: int) -> str: + """ + Time complexity: O(n) + Breakdown: + - s.split(): Splits the string into a list of words - O(n) where n is the length of the string + - [:k]: Slices the first k elements from the list - O(k) where k is the number of words to keep + - ' '.join(): Joins k words back into a string - O(n) to construct the result string + Since k <= n (the number of words cannot exceed the string length), overall time complexity is O(n) + + Space complexity: O(n) + Breakdown: + - The split() operation creates a list of all words in the string - O(n) space + - The join() operation creates a new string for the result - O(n) space + Overall: O(n) additional space + """ + return ' '.join(s.split()[:k]) diff --git a/problems/easy/truncate_sentence_1816/test_solution.py b/problems/easy/truncate_sentence_1816/test_solution.py new file mode 100644 index 0000000..269fee7 --- /dev/null +++ b/problems/easy/truncate_sentence_1816/test_solution.py @@ -0,0 +1,15 @@ +import pytest +from tests.base_test import BaseTestSolution +from .solution import Solution + + +class TestSolution(BaseTestSolution): + solution = Solution() + + @pytest.mark.parametrize("method_name, s, k, expected, timeout", [ + ('truncate_sentence', "Hello how are you Contestant", 4, "Hello how are you", None), + ('truncate_sentence', "What is the solution to this problem", 4, "What is the solution", None), + ('truncate_sentence', "chopper is not a tanuki", 5, "chopper is not a tanuki", None), + ]) + def test_truncate_sentence(self, method_name, s, k, expected, timeout): + self._run_test(self.solution, method_name, (s, k,), expected, timeout)