Given a string s consisting of words and spaces, return the length of the last word in the string.
A word is a maximal substring consisting of non-space characters only.
Example 1:
Input: s = "Hello World" Output: 5 Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon " Output: 4 Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy" Output: 6 Explanation: The last word is "joyboy" with length 6.
Constraints:
1 <= s.length <= 104s consists of only English letters and spaces ' '.s.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to find the length of the last word means going through the entire input and checking every possible scenario. We identify words by looking for spaces, and isolate the final word by eliminating any trailing spaces.
Here's how the algorithm would work step-by-step:
def length_of_last_word_brute_force(input_string):
string_length = len(input_string)
last_word_length = 0
# Start from the end of the string
current_index = string_length - 1
# Skip trailing spaces
while current_index >= 0 and input_string[current_index] == ' ':
current_index -= 1
# Count the length of the last word
while current_index >= 0 and input_string[current_index] != ' ':
last_word_length += 1
current_index -= 1
return last_word_lengthThe most efficient way to find the length of the last word is to start from the end of the sentence and work backwards. We can ignore any trailing spaces and stop counting characters once we hit the beginning of the last word or the start of the sentence.
Here's how the algorithm would work step-by-step:
def length_of_last_word(sentence):
sentence_length = len(sentence)
last_word_length = 0
# Start from the end of the sentence.
for i in range(sentence_length - 1, -1, -1):
if sentence[i] != ' ':
# Found a letter, start counting.
last_word_length += 1
else:
# If we have counted some letters
if last_word_length > 0:
# Then we've found the end of the word
return last_word_length
# Handle the case where there are no spaces
return last_word_length| Case | How to Handle |
|---|---|
| Null or empty input string | Return 0 immediately as there are no words. |
| String with only spaces | Return 0 because there are no non-space words. |
| String with leading and trailing spaces | Trim the string before processing to remove extra spaces. |
| String with multiple spaces between words | Trimmed string will now have one space, or we can iterate backwards skipping spaces. |
| String with a single word and no spaces | The length of the word is the length of the entire string. |
| Very long string to check performance/efficiency | Iterating backwards provides O(n) linear time complexity which scales appropriately. |
| String ending with multiple spaces after last word | Trim the string to remove them, or check for space before counting. |
| String with unicode characters | Ensure the language properly handles unicode string length and character access. |