From d85c129f19e82d797d496df9878b787611eeefd5 Mon Sep 17 00:00:00 2001 From: Gopal Gupta Date: Sat, 8 Nov 2025 10:59:06 +0530 Subject: [PATCH 1/2] Implement LengthOfLastWord algorithm in strings package --- .../strings/LengthOfLastWord.java | 51 +++++++++++++++++++ .../strings/LengthOfLastWordTest.java | 4 ++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/com/thealgorithms/strings/LengthOfLastWord.java create mode 100644 src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java diff --git a/src/main/java/com/thealgorithms/strings/LengthOfLastWord.java b/src/main/java/com/thealgorithms/strings/LengthOfLastWord.java new file mode 100644 index 000000000000..7eed59a5ef99 --- /dev/null +++ b/src/main/java/com/thealgorithms/strings/LengthOfLastWord.java @@ -0,0 +1,51 @@ +package com.thealgorithms.strings; + +/** + * The {@code LengthOfLastWord} class provides a utility method to determine + * the length of the last word in a given string. + * + *

A "word" is defined as a maximal substring consisting of non-space + * characters only. Trailing spaces at the end of the string are ignored. + * + *

Example: + *

{@code
+ * LengthOfLastWord obj = new LengthOfLastWord();
+ * System.out.println(obj.lengthOfLastWord("Hello World"));  // Output: 5
+ * System.out.println(obj.lengthOfLastWord("  fly me   to   the moon  "));  // Output: 4
+ * System.out.println(obj.lengthOfLastWord("luffy is still joyboy"));  // Output: 6
+ * }
+ * + *

This implementation runs in O(n) time complexity, where n is the length + * of the input string, and uses O(1) additional space. + */ +public class LengthOfLastWord { + + /** + * Returns the length of the last word in the specified string. + * + *

The method iterates from the end of the string, skipping trailing + * spaces first, and then counts the number of consecutive non-space characters + * characters until another space (or the beginning of the string) is reached. + * + * @param s the input string to analyze + * @return the length of the last word in {@code s}; returns 0 if there is no word + * @throws NullPointerException if {@code s} is {@code null} + */ + public int lengthOfLastWord(String s) { + int sizeOfString = s.length() - 1; + int lastWordLength = 0; + + // Skip trailing spaces from the end of the string + while (sizeOfString >= 0 && s.charAt(sizeOfString) == ' ') { + sizeOfString--; + } + + // Count the characters of the last word + while (sizeOfString >= 0 && s.charAt(sizeOfString) != ' ') { + lastWordLength++; + sizeOfString--; + } + + return lastWordLength; + } +} diff --git a/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java b/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java new file mode 100644 index 000000000000..191b264c01e1 --- /dev/null +++ b/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java @@ -0,0 +1,4 @@ +package com.thealgorithms.strings; + +public class LengthOfLastWordTest { +} From 7068e548dc1c9443bb276820598633eb830db2c3 Mon Sep 17 00:00:00 2001 From: Gopal Gupta Date: Sat, 8 Nov 2025 10:59:31 +0530 Subject: [PATCH 2/2] Add JUnit tests for LengthOfLastWord algorithm --- .../strings/LengthOfLastWordTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java b/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java index 191b264c01e1..a9bb1453bf71 100644 --- a/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java +++ b/src/test/java/com/thealgorithms/strings/LengthOfLastWordTest.java @@ -1,4 +1,18 @@ package com.thealgorithms.strings; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + public class LengthOfLastWordTest { + @Test + public void testLengthOfLastWord() { + assertEquals(5, new LengthOfLastWord().lengthOfLastWord("Hello World")); + assertEquals(4, new LengthOfLastWord().lengthOfLastWord(" fly me to the moon ")); + assertEquals(6, new LengthOfLastWord().lengthOfLastWord("luffy is still joyboy")); + assertEquals(5, new LengthOfLastWord().lengthOfLastWord("Hello")); + assertEquals(0, new LengthOfLastWord().lengthOfLastWord(" ")); + assertEquals(0, new LengthOfLastWord().lengthOfLastWord("")); + assertEquals(3, new LengthOfLastWord().lengthOfLastWord("JUST LIE ")); + } }