VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-smallest-largest-word-string/

⇱ Program to find Smallest and Largest Word in a String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find Smallest and Largest Word in a String

Last Updated : 3 Mar, 2025

Given a string, find the minimum and the maximum length words in it. 

Examples:

Input : "This is a test string"
Output : Minimum length word: a
Maximum length word: string
Input : "GeeksforGeeks A computer Science portal for Geeks"
Output : Minimum length word: A
Maximum length word: GeeksforGeeks

Method 1:

The idea is to keep a starting index si and an ending index ei

  • si points to the starting of a new word and we traverse the string using ei.
  • Whenever a space or ‘\0’ character is encountered,we compute the length of the current word using (ei - si) and compare it with the minimum and the maximum length so far. 
    • If it is less, update the min_length and the min_start_index( which points to the starting of the minimum length word).
    • If it is greater, update the max_length and the max_start_index( which points to the starting of the maximum length word).
  • Finally, update minWord and maxWord which are output strings that have been sent by reference with the substrings starting at min_start_index and max_start_index of length min_length and max_length respectively.

Below is the implementation of the above approach:


Output
Minimum length word: A
Maximum length word: GeeksforGeeks

Time Complexity: O(n), where n is the length of string.
Auxiliary Space: O(n), where n is the length of string. This is because when string is passed in the function it creates a copy of itself in stack.

Method 2: By Using Regular Expressions

In this approach we uses regular expressions to find words in a given input string and iterates through them. It keeps track of the smallest and largest words based on their lengths and prints them.

  • First, Define a regular expression pattern to match words.
  • Then, Create iterators to search for words within the input string.
  • Initialize variables to store the smallest and largest words.
  • Iterate through the words in the string.
  • Check if the current word is smaller than the smallest word.
  • Check if the current word is larger than the largest word .
  • At the end ,print the smallest and largest words.

Output
Minimum length word: a
Maximum length word: string

Time complexity: O(n), n is length of string
Space complexity: O(m), m is the length of the longest word.

Method 3: Using Stack

In this approach, we will push all the words one by one into a char stack and check for the max as well as min length for every word on the basis of that we will print the Minimum length word and Maximum length word.

Below is the implementation of the above approach:


Output
Minimum length word: A
Maximum length word: GeeksforGeeks

Time complexity: O(n), n is length of string.
Auxiliary Space: O(m), m is the length of the longest word.

Comment
Article Tags:
Article Tags: