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:
C++
#include<iostream>
#include<cstring>
using namespace std;
void minMaxLengthWords(string input, string &minWord, string &maxWord)
{
int len = input.length();
int si = 0, ei = 0;
int min_length = len, min_start_index = 0, max_length = 0, max_start_index = 0;
while (ei <= len)
{
if (ei < len && input[ei] != ' ' )
ei++;
else
{
int curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
minWord = input.substr(min_start_index, min_length);
maxWord = input.substr(max_start_index, max_length);
}
int main()
{
string a = "GeeksforGeeks A Computer Science portal for Geeks" ;
string minWord, maxWord;
minMaxLengthWords(a, minWord, maxWord);
cout << "Minimum length word: "
<< minWord << endl
<< "Maximum length word: "
<< maxWord << endl;
}
|
Java
import java.io.*;
class GFG
{
static String minWord = "" , maxWord = "" ;
static void minMaxLengthWords(String input)
{
input=input.trim();
int len = input.length();
int si = 0 , ei = 0 ;
int min_length = len, min_start_index = 0 ,
max_length = 0 , max_start_index = 0 ;
while (ei <= len)
{
if (ei < len && input.charAt(ei) != ' ' )
{
ei++;
}
else
{
int curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
minWord = input.substring(min_start_index, min_start_index + min_length);
maxWord = input.substring(max_start_index, max_start_index+max_length);
}
public static void main(String[] args)
{
String a = "GeeksforGeeks A Computer Science portal for Geeks" ;
minMaxLengthWords(a);
System.out.print( "Minimum length word: "
+ minWord
+ "\nMaximum length word: "
+ maxWord);
}
}
|
Python 3
def minMaxLengthWords(inp):
length = len (inp)
si = ei = 0
min_length = length
min_start_index = max_length = max_start_index = 0
while ei < = length:
if (ei < length) and (inp[ei] ! = " " ):
ei + = 1
else :
curr_length = ei - si
if curr_length < min_length:
min_length = curr_length
min_start_index = si
if curr_length > max_length:
max_length = curr_length
max_start_index = si
ei + = 1
si = ei
minWord = inp[min_start_index :
min_start_index + min_length]
maxWord = inp[max_start_index : max_length]
print ( "Minimum length word: " , minWord)
print ( "Maximum length word: " , maxWord)
a = "GeeksforGeeks A Computer Science portal for Geeks"
minMaxLengthWords(a)
|
C#
using System;
class GFG
{
static String minWord = "" , maxWord = "" ;
static void minMaxLengthWords(String input)
{
int len = input.Length;
int si = 0, ei = 0;
int min_length = len, min_start_index = 0,
max_length = 0, max_start_index = 0;
while (ei <= len)
{
if (ei < len && input[ei] != ' ' )
{
ei++;
}
else
{
int curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
minWord = input.Substring(min_start_index, min_length);
maxWord = input.Substring(max_start_index, max_length);
}
public static void Main(String[] args)
{
String a = "GeeksforGeeks A Computer Science portal for Geeks" ;
minMaxLengthWords(a);
Console.Write( "Minimum length word: "
+ minWord
+ "\nMaximum length word: "
+ maxWord);
}
}
|
Javascript
<script>
let minWord = "" ;
let maxWord = "" ;
function minMaxLengthWords(input)
{
let len = input.length;
let si = 0, ei = 0;
let min_length = len;
let min_start_index = 0;
let max_length = 0;
let max_start_index = 0;
while (ei <= len)
{
if (ei < len && input[ei] != ' ' )
{
ei++;
}
else
{
let curr_length = ei - si;
if (curr_length < min_length)
{
min_length = curr_length;
min_start_index = si;
}
if (curr_length > max_length)
{
max_length = curr_length;
max_start_index = si;
}
ei++;
si = ei;
}
}
minWord =
input.substring(min_start_index,min_start_index + min_length);
maxWord =
input.substring(max_start_index, max_length);
}
let a = "GeeksforGeeks A Computer Science portal for Geeks" ;
minMaxLengthWords(a);
document.write( "Minimum length word: "
+ minWord+ "<br>"
+ "Maximum length word: "
+ maxWord);
</script>
|
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!