Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb33488

Browse files
authored
Implement LengthOfLastWord algorithm and add JUnit tests (#7057)
* Implement LengthOfLastWord algorithm in strings package* Add JUnit tests for LengthOfLastWord algorithm* style: fix import order and spacing for clang-format
1 parent48e02b3 commitbb33488

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packagecom.thealgorithms.strings;
2+
3+
/**
4+
* The {@code LengthOfLastWord} class provides a utility method to determine
5+
* the length of the last word in a given string.
6+
*
7+
* <p>A "word" is defined as a maximal substring consisting of non-space
8+
* characters only. Trailing spaces at the end of the string are ignored.
9+
*
10+
* <p><strong>Example:</strong>
11+
* <pre>{@code
12+
* LengthOfLastWord obj = new LengthOfLastWord();
13+
* System.out.println(obj.lengthOfLastWord("Hello World")); // Output: 5
14+
* System.out.println(obj.lengthOfLastWord(" fly me to the moon ")); // Output: 4
15+
* System.out.println(obj.lengthOfLastWord("luffy is still joyboy")); // Output: 6
16+
* }</pre>
17+
*
18+
* <p>This implementation runs in O(n) time complexity, where n is the length
19+
* of the input string, and uses O(1) additional space.
20+
*/
21+
publicclassLengthOfLastWord {
22+
23+
/**
24+
* Returns the length of the last word in the specified string.
25+
*
26+
* <p>The method iterates from the end of the string, skipping trailing
27+
* spaces first, and then counts the number of consecutive non-space characters
28+
* characters until another space (or the beginning of the string) is reached.
29+
*
30+
* @param s the input string to analyze
31+
* @return the length of the last word in {@code s}; returns 0 if there is no word
32+
* @throws NullPointerException if {@code s} is {@code null}
33+
*/
34+
publicintlengthOfLastWord(Strings) {
35+
intsizeOfString =s.length() -1;
36+
intlastWordLength =0;
37+
38+
// Skip trailing spaces from the end of the string
39+
while (sizeOfString >=0 &&s.charAt(sizeOfString) ==' ') {
40+
sizeOfString--;
41+
}
42+
43+
// Count the characters of the last word
44+
while (sizeOfString >=0 &&s.charAt(sizeOfString) !=' ') {
45+
lastWordLength++;
46+
sizeOfString--;
47+
}
48+
49+
returnlastWordLength;
50+
}
51+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagecom.thealgorithms.strings;
2+
3+
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
4+
5+
importorg.junit.jupiter.api.Test;
6+
7+
publicclassLengthOfLastWordTest {
8+
@Test
9+
publicvoidtestLengthOfLastWord() {
10+
assertEquals(5,newLengthOfLastWord().lengthOfLastWord("Hello World"));
11+
assertEquals(4,newLengthOfLastWord().lengthOfLastWord(" fly me to the moon "));
12+
assertEquals(6,newLengthOfLastWord().lengthOfLastWord("luffy is still joyboy"));
13+
assertEquals(5,newLengthOfLastWord().lengthOfLastWord("Hello"));
14+
assertEquals(0,newLengthOfLastWord().lengthOfLastWord(" "));
15+
assertEquals(0,newLengthOfLastWord().lengthOfLastWord(""));
16+
assertEquals(3,newLengthOfLastWord().lengthOfLastWord("JUST LIE "));
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp