Python program to find the longest word in a sentence
Given a string S consisting of lowercase English alphabets, the task is to print the longest word present in the given string in python.
Examples:
Input: S = “be confident and be yourself”
Output: “confident”
Explanation:
Words present in the sentence are “be”, “confident”, “and”, “be” and “yourself”. Length of each of the words are 2, 9, 3, 2, and 8 respectively.
Therefore, the longest word is “confident”.Input: S = “geeks for geeks”
Output: “geeks”
Searching-based Approach: Refer to this article to solve this problem by performing the following steps:
- Iterate over the characters of the string.
- Check if the current character encountered is a space or end of the string is reached.
- If the current character is found to be so, update the maximum length of a word obtained.
- Finally, print the longest word obtained using substr() method.
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach using sorted() method: The idea is to split the string into separate words and store in a list. Then, sort the list in the order of increasing length using the sorted() method. Print the last string in the sorted list.
Below is the implementation of the above approach:
Python3
# Python3 program for the above approach # Function to print the longest # word in given sentence def largestWord(s): # Sort the words in increasing # order of their lengths s = sorted (s, key = len ) # Print last word print (s[ - 1 ]) # Driver Code if __name__ = = "__main__" : # Given string s = "be confident and be yourself" # Split the string into words l = list (s.split( " " )) largestWord(l) |
confident
Time Complexity: O(N * logN)
Auxiliary Space: O(1)
Approach using max() and split() methods: This approach uses the max() function with the split () method to split the string into a list of words and find the longest word.
Python3
# Python3 program for the above approach # Function to print the # longest word in the string s def longestword(s): #splitting the string into words s = s.split() # Using max() method print ( max (s,key = len )) # Driver Code if __name__ = = "__main__" : # Given string s = "be confident and be yourself" #calling longestword function longestword(s) #This code contributed by tvsk |
confident
Time Complexity: O(N), N is the number of words in the input string used for iteration only once.
Auxiliary Space: O(N), N is used for creating a new list from the input string and stores it in memory.
Approach using re module:
Python3
import re def longest_word(sentence): words = re.findall(r '\b\w+\b' , sentence) # find all words in the sentence using regular expression return max (words, key = len ) # return the word with the maximum length sentence = "be confident and be yourself" print (longest_word(sentence)) |
confident
Time Complexity: O(N), N is the number of words in the input string used for iteration only once.
Auxiliary Space: O(N), N is used for creating a new list from the input string and stores it in memory.
Approach Using the reduce() function from the functools module:
Algorithm:
1.Split the input string into a list of words using the split() method.
2.Initialize a variable to store the longest word as the first word in the list.
3.Iterate through the list of words and compare the length of each word to the length of the current longest word.
4.If the length of the current word is greater than the length of the current longest word, update the variable storing the longest word.
5.Return the longest word.
Python3
from functools import reduce # function to find the longest word in a string def longest_word(s): # split the string into words words = s.split() # use reduce to find the word with the maximum length return reduce ( lambda x, y: x if len (x) > len (y) else y, words) # example usage s = "be confident and be yourself" longest = longest_word(s) print ( "The longest word in the string is:" , longest) #This code is contributed by Jyothi pinjala |
The longest word in the string is: confident
Time Complexity: O(n), where n is the total number of characters in the input string. This is because we need to iterate through each character in the string to split it into words, and then iterate through each word to find the longest one.
Space Complexity: O(m), where m is the total number of words in the input string. This is because we need to store each word in a list, and then store the longest word as a variable.
Please Login to comment...