forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLengthOfLastWord.java
More file actions
51 lines (46 loc) · 1.85 KB
/
LengthOfLastWord.java
File metadata and controls
51 lines (46 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.
*
* <p>A "word" is defined as a maximal substring consisting of non-space
* characters only. Trailing spaces at the end of the string are ignored.
*
* <p><strong>Example:</strong>
* <pre>{@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
* }</pre>
*
* <p>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.
*
* <p>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;
}
}