Open In App

Python program to find the smallest word in a sentence

Last Updated : 06 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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)


Output:

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)


Output

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):
      
    #splitting 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


Output

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


Output

is

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:

  1. Split the given words by using split() function.
  2. Create an array words_len to store the length of each word. 
  3. Find the minimum length of words in list words_len.
  4. Using index() function, the minimum length word is found.

Python3




# Python3 program for the above approach
 
# Function to print the
# smallest word in the string s
 
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])
 
 
# Driver Code
if __name__ == "__main__":
 
    # Given string
    s = "sky is blue"
 
    #calling smallestWord function
    smallestWord(s)
     
 
# This Code is contributed by Pratik Gupta (guptapratik)


Output

is

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):
    # Split the input string into words
    words = s.split()
 
    # Use the argmin function from the NumPy library to find the index of the minimum length word
    min_word_index = np.argmin([len(word) for word in words])
 
    # Return the minimum length word
    return words[min_word_index]
s = "sky is blue"
smallest = smallestWord(s)
print("The smallest word is:", smallest)
#This code is contributed  by Jyothi pinjala.


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads