Given a string, the task is to write a Python program to print the last word in that string.
Examples:
Input: sky is blue in color
Output: color
Explanation: color is last word in the sentence.
Input: Learn algorithms at geeksforgeeks
Output: geeksforgeeks
Explanation: geeksforgeeks is last word in the sentence.
Approach #1: Using For loop + String Concatenation
- Scan the sentence
- Take an empty string, newstring.
- Traverse the string in reverse order and add character to newstring using string concatenation.
- Break the loop till we get first space character.
- Reverse newstring and return it (it is the last word in the sentence).
Below is the implementation of the above approach:
Python3
def lastWord(string):
newstring = ""
length = len(string)
for i in range(length-1, 0, -1):
if(string[i] == " "):
return newstring[::-1]
else:
newstring = newstring + string[i]
string = "Learn algorithms at geeksforgeeks"
print(lastWord(string))
|
Output:
geeksforgeeks
Approach #2: Using split() method
- As all the words in a sentence are separated by spaces.
- We have to split the sentence by spaces using split().
- We split all the words by spaces and store them in a list.
- The last element in the list is the answer
Below is the implementation of the above approach:
Python3
def lastWord(string):
lis = list(string.split(" "))
length = len(lis)
return lis[length-1]
string = "Learn algorithms at geeksforgeeks"
print(lastWord(string))
|
Output:
geeksforgeeks
The time and space complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach #3: Using rfind() method
In this approach, we use the rfind() method to find the index of the last space in the string.
The part of the string from the last space to the end of the string is the last word.
Below is the implementation of the above approach:
Python3
def lastWord(string):
index = string.rfind(" ")
return string[index+1:]
string = "Learn algorithms at geeksforgeeks"
print(lastWord(string))
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach #4: Using reversed() function:
Algorithm:
- Reverse the given string.
- Find the index of the first space in the reversed string.
- Return the last word in the original string by taking a slice from the original string starting from the end of the
- reversed string to the position of the first space in the reversed string.
Python3
def lastWord(string):
reversed_string = string[::-1]
index = reversed_string.find(" ")
return string[-index-1:]
string = "Learn algorithms at geeksforgeeks"
print(lastWord(string))
|
Time Complexity:
The time complexity of the find() function used in the implementation is O(n), where n is the length of the string. The slicing operation in Python also takes O(n) time complexity. Therefore, the overall time complexity of this algorithm is O(n).
Space Complexity:
The space complexity of this algorithm is O(n), where n is the length of the string. This is because we are creating a new string object by reversing the original string. However, the space complexity can be further optimized by iterating over the string from the end to find the last word instead of reversing the entire string
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!