Given a string S contains of upper/lower case alphabets and empty space characters ‘ ‘, return the length of last word (last word means the last appearing word if we loop from left to right) in the string
If the last word does not exist, return 0
Note: A word is defined as a maximal substring consisting of non space characters only
Example:
I/P: “Hello world”
O/P: 5
Step 1: Remove the white spaces from both sides
Step 2: Split the string into list based on spaces
Step 3: Return 0 if length of the list is 0 else find the last index using – 1 and return its length.
Time Complexity:
For striping spaces it takes O(n) time.
Returning len of the cast index O(length of the last index)
Overall: O(n) + O(length of the last index)