The problem is to go through all the words in an array and the program should return the word with the longest one. Consider for example we are having an array, and we have numbers in alphabetic form, now when we pass this array as an input then we should get the word with the longest one. Below I had explained it with an example in order to give an elaborate view.
Example:
Input : [“one”, “two”, “three”, “four”]
Output: three
Explanation:
As we pass the above array as a input, our program will check which word or which value has the maximum length and after completely iterating through the array then it returns the word with longest length.
Methods 1#: Iterating and finding the greater length word.
Approach:
- First declare a function with the name ‘longest Length ‘ which accepts a list as an argument.
- Now, take a list where you have all the values.
- We are going to take this list, and we will iterate through each item using for loop.
- Then we take two variables max1 and temp to store the maximum length and the word with the longest length.
- After completing the above steps then we take the first value in the list and the first value length in order to compare.
- Once the above steps are completed we compare the items in the list using for loop. Below I had mentioned the logic.
- After completing the above steps run the program, and then we’ll get the required result.
Below is the implementation of the above approach:
Python3
# function to find the longest # length in the list def longestLength(a): max1 = len (a[ 0 ]) temp = a[ 0 ] # for loop to traverse the list for i in a: if ( len (i) > max1): max1 = len (i) temp = i print ( "The word with the longest length is:" , temp, " and length is " , max1) # Driver Program a = [ "one" , "two" , "third" , "four" ] longestLength(a) |
The word with the longest length is: third and length is 5
Time Complexity: O(n)
Auxiliary Space: O(n)
Methods 2#: Using sort().
Approach:
- First declare a function with the name ‘longest Length ‘ which accepts a list as an argument.
- Now, take a list where you have all the values.
- We are going to take this list, and we will iterate through each item using for loop.
- Then we take an empty list with the name final List and will append all the items.
- After appending we will sort the list using the sort() method.
- Now, as the list is sorted we can get the longest length, and we can display it.
- After completing the above steps run the program, and then we’ll get the required result.
Below is the implementation of the above approach:
Python3
# function to find the longest length in the list def longestLength(words): finalList = [] for word in words: finalList.append(( len (word), word)) finalList.sort() print ( "The word with the longest length is:" , finalList[ - 1 ][ 1 ], " and length is " , len (finalList[ - 1 ][ 1 ])) # Driver Program a = [ "one" , "two" , "third" , "four" ] longestLength(a) |
The word with the longest length is: third and length is 5
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach using max() method: This approach uses the max() function with key arguments and find the longest word.
Python3
# function to find the longest length in the list def longestLength(words): res = max (words,key = len ) print ( "The word with the longest length is:" , res, " and length is " , len (res)) # Driver Program a = [ "one" , "two" , "third" , "four" ] longestLength(a) #this code contributed by tvsk |
The word with the longest length is: third and length is 5
Time Complexity: O(n)
Auxiliary Space: O(n)
Approach using reduce() method: Here’s a solution to find the longest word in the list of words using the reduce function from the functools module:
length of the longest word from the list of words using the reduce() method:
Inputs: List of words, a
Output: Length of the longest word in the list, longest_word_length
Import the reduce() function from the functools module.
Define a function longestLength that takes in a list of words as an argument.
Inside the longestLength function, use the reduce() function to compare the length of the words in the list.
The reduce() function takes two arguments: a function that performs the comparison and the list of words.
The function passed to reduce() checks the length of two words and returns the word with the longest length.
Store the result of the reduce() function in the variable longest_word.
Find the length of the longest_word using the built-in len() function and store it in the variable longest_word_length.
Print the longest word and its length using the print() function.
Call the longestLength() function with a list of words as an argument.
The program returns the length of the longest word in the list.
Python3
from functools import reduce def longestLength(words): # using reduce to find the longest word longest_word = reduce ( lambda x, y: x if len (x) > len (y) else y, words) print ( "The word with the longest length is:" , longest_word, " and length is " , len (longest_word)) # Driver Program a = [ "one" , "two" , "third" , "four" ] longestLength(a) |
The word with the longest length is: third and length is 5
Time Complexity:
The time complexity of this program is O(n), where n is the number of words in the list. This is because the reduce() function goes through each word in the list only once to find the longest word.
Auxiliary Space Complexity:
The auxiliary space complexity of this program is O(1). This is because only a few variables are being created and the size of the input does not affect the space complexity. The space used by the reduce() function is also constant as it only stores one value at a time.