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
def smallestWord(s):
s = sorted (s, key = len )
print (s[ 0 ])
if __name__ = = "__main__" :
s = "sky is blue"
l = list (s.split( " " ))
smallestWord(l)
|
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
import re
from functools import reduce
def smallestWord(s):
s = re.findall(r '[a-zA-Z]+' , s)
ans = reduce ( lambda a, b : a if len (a) < len (b) else b, s)
print (ans)
if __name__ = = "__main__" :
s = "sky is blue"
smallestWord(s)
|
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
def smallestWord(s):
s = s.split()
print ( min (s,key = len ))
if __name__ = = "__main__" :
s = "sky is blue"
smallestWord(s)
|
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
sentence = "sky is blue"
words = sentence.split()
smallest = reduce ( lambda x, y: x if len (x) < len (y) else y, words)
print (smallest)
|
Time Complexity: O(N), N is the number of words in the input string used for iteration only once.
Auxiliary Space: O(1)
Approach using list, split(), min(), index():
Approach:
- Split the given words by using split() function.
- Create an array words_len to store the length of each word.
- Find the minimum length of words in list words_len.
- Using index() function, the minimum length word is found.
Python3
def smallestWord(s):
words = s.split()
words_len = []
for i in words:
words_len.append( len (i))
minimum_len_word = min (words_len)
index_char = words_len.index(minimum_len_word)
print (words[index_char])
if __name__ = = "__main__" :
s = "sky is blue"
smallestWord(s)
|
Time complexity: O(n)
Auxiliary Space: O(n)
Approach Using the numpy module’s argmin() function:
Algorithm:
1.Split the input string s into individual words using the split() method.
2.Compute the length of each word in the list using a list comprehension.
3.Find the index of the minimum length word in the list of word lengths using NumPy’s argmin() function.
4.Return the word at the index found in step 3.
Python3
import numpy as np
def smallestWord(s):
words = s.split()
min_word_index = np.argmin([ len (word) for word in words])
return words[min_word_index]
s = "sky is blue"
smallest = smallestWord(s)
print ( "The smallest word is:" , smallest)
|
Output:
The smallest word is: is
Time Complexity:
The time complexity of the function is O(n log n) because the split() method takes O(n) time to split the string into words, and the argmin() function takes O(n log n) time to find the index of the minimum length word in the list of word lengths. The list comprehension to compute the word lengths takes O(n) time, which is dominated by the time complexity of the argmin() function.
Space Complexity:
The space complexity of the function is O(n) because we create a list of word lengths with length n, which requires O(n) space. The other variables used in the function, such as words and min_word_index, require O(1) space.
Last Updated :
06 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...