![]() |
VOOZH | about |
Given a string, find the minimum and the maximum length words in it.
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
The idea is to keep a starting index si and an ending index ei.
Below is the implementation of the above approach:
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.
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.
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.
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:
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.