From e0df462edb90199bd4543b552dba642e67234a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:31:45 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=2058=20(Length=20?= =?UTF-8?q?of=20Last=20Word)=20from=20TypeScript=20to=20C++23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces typescript/lengthOfLastWord.ts with solutions/0001-0100/0058-length-of-last-word/. Uses a single backward scan instead of trim+split, avoiding the intermediate substrings. --- .../0058-length-of-last-word/README.md | 11 ++++++++++ .../0058-length-of-last-word/solution.cpp | 21 +++++++++++++++++++ .../0058-length-of-last-word/solution.hpp | 11 ++++++++++ .../0058-length-of-last-word/test.cpp | 9 ++++++++ typescript/lengthOfLastWord.ts | 5 ----- 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 solutions/0001-0100/0058-length-of-last-word/README.md create mode 100644 solutions/0001-0100/0058-length-of-last-word/solution.cpp create mode 100644 solutions/0001-0100/0058-length-of-last-word/solution.hpp create mode 100644 solutions/0001-0100/0058-length-of-last-word/test.cpp delete mode 100644 typescript/lengthOfLastWord.ts diff --git a/solutions/0001-0100/0058-length-of-last-word/README.md b/solutions/0001-0100/0058-length-of-last-word/README.md new file mode 100644 index 0000000..de393a7 --- /dev/null +++ b/solutions/0001-0100/0058-length-of-last-word/README.md @@ -0,0 +1,11 @@ +# 58. Length of Last Word + +[LeetCode problem 58](https://leetcode.com/problems/length-of-last-word/) + +- **Difficulty**: Easy +- **Tags**: string + +## Description + +Given a string `s` of words and spaces, return the length of the last word +(a maximal substring of non-space characters). diff --git a/solutions/0001-0100/0058-length-of-last-word/solution.cpp b/solutions/0001-0100/0058-length-of-last-word/solution.cpp new file mode 100644 index 0000000..52fc1f6 --- /dev/null +++ b/solutions/0001-0100/0058-length-of-last-word/solution.cpp @@ -0,0 +1,21 @@ +#include "solution.hpp" + +namespace leetcode::p0058 { + +int solve(const std::string& s) { + int i = static_cast(s.size()) - 1; + + while (i >= 0 && s.at(static_cast(i)) == ' ') { + --i; + } + + int length = 0; + while (i >= 0 && s.at(static_cast(i)) != ' ') { + ++length; + --i; + } + + return length; +} + +} // namespace leetcode::p0058 diff --git a/solutions/0001-0100/0058-length-of-last-word/solution.hpp b/solutions/0001-0100/0058-length-of-last-word/solution.hpp new file mode 100644 index 0000000..cdfb28e --- /dev/null +++ b/solutions/0001-0100/0058-length-of-last-word/solution.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace leetcode::p0058 { + +// optimized: O(n) time, O(1) space — scan backward from the end, skipping +// trailing spaces then counting the last word. +int solve(const std::string& s); + +} // namespace leetcode::p0058 diff --git a/solutions/0001-0100/0058-length-of-last-word/test.cpp b/solutions/0001-0100/0058-length-of-last-word/test.cpp new file mode 100644 index 0000000..4f9dda8 --- /dev/null +++ b/solutions/0001-0100/0058-length-of-last-word/test.cpp @@ -0,0 +1,9 @@ +#include + +#include "solution.hpp" + +TEST_CASE("Problem 58", "[p0058]") { + CHECK(leetcode::p0058::solve("Hello World") == 5); + CHECK(leetcode::p0058::solve(" fly me to the moon ") == 4); + CHECK(leetcode::p0058::solve("luffy is still joyboy") == 6); +} diff --git a/typescript/lengthOfLastWord.ts b/typescript/lengthOfLastWord.ts deleted file mode 100644 index f6ccca7..0000000 --- a/typescript/lengthOfLastWord.ts +++ /dev/null @@ -1,5 +0,0 @@ -function lengthOfLastWord(s: string): number { - const strSplited = s.trim().split(" "); - const lastWord = strSplited[strSplited.length - 1]; - return lastWord.length; -}