Python program to find the smallest word in a sentence
Given a string S of lowercase English alphabets, the task is to print the smallest word in the given string.
Examples:
Input: S = “sky is blue”
Output: “is”
Explanation:
Length of “sky” is 3.
Length of is “is” 2.
Length of “blue” is 4.
Therefore, the smallest word is “is”.Input: S = “geeks for geeks”
Output: “for”
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 minimum length of a word obtained.
- Finally, print the smallest word obtained using substr() method.
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
Approach using sorted() method: The idea is to split the words of the string into a list and sort the list in increasing order of length of words using the sorted() method. Finally, print the first string present in the list.
Below is the implementation of the above approach:
Python3
# Python3 program for the above approach # Function to print the # smallest word in the string s def smallestWord(s): # Using sorted() method s = sorted (s, key = len ) # Print first word print (s[ 0 ]) # Driver Code if __name__ = = "__main__" : # Given string s = "sky is blue" # Convert string to list l = list (s.split( " " )) smallestWord(l) |
is
Time Complexity: O(N * LogN)
Auxiliary Space: O(1)
An Approach using re.findall() and reduce method: The idea is to find all words and make list with them and use reduce method to get the smallest string from the list.
Below is the implementation of the above approach:
Python
# Python3 program for the above approach import re from functools import reduce # Function to print the # smallest word in the string s def smallestWord(s): # Using sorted() method s = re.findall(r '[a-zA-Z]+' , s) ans = reduce ( lambda a, b : a if len (a) < len (b) else b, s) # Print smallest word print (ans) # Driver Code if __name__ = = "__main__" : # Given string s = "sky is blue" # Printing smallerst element from string smallestWord(s) |
is
Time Complexity: O(N * Log N) for using the sorted method, where N is the length of the string.
Auxiliary Space: O(1), no extra space is required.
Approach using min() and split() methods: This approach uses the min () function with the split () method to split the string into a list of words and find the smallest word.
Python3
# Python3 program for the above approach # Function to print the # smallest word in the string s def smallestWord(s): #spliting the string into words s = s.split() # Using min() method print ( min (s,key = len )) # Driver Code if __name__ = = "__main__" : # Given string s = "sky is blue" #calling smallestWord function smallestWord(s) #This code contributed by tvsk |
is
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 reduce() and split() methods:
We use the reduce() function to iterate through the words and find the smallest word.
Python3
from functools import reduce #importing reduce function from functools sentence = "sky is blue" #initializing the sentence words = sentence.split() #splitting the sentence into words smallest = reduce ( lambda x, y: x if len (x) < len (y) else y, words) #reducing the words using lambda function and finding the smallest word print (smallest) #printing the smallest word |
is
Time Complexity: O(N), N is the number of words in the input string used for iteration only once.
Auxiliary Space: O(1)
Please Login to comment...