Program to find Smallest and Largest Word in a String
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.
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.
C++
#include <iostream>
#include <string>
#include <regex>
#include <iterator>
void findSmallestLargestWordsRegex( const std::string& input) {
std::regex wordRegex( "\\b\\w+\\b" );
std::sregex_iterator wordsBegin(input.begin(), input.end(), wordRegex);
std::sregex_iterator wordsEnd;
std::string smallestWord, largestWord;
for (std::sregex_iterator it = wordsBegin; it != wordsEnd; ++it) {
std::smatch match = *it;
std::string word = match.str();
if (word.length() < smallestWord.length() || smallestWord.empty()) {
smallestWord = word;
}
if (word.length() > largestWord.length()) {
largestWord = word;
}
}
std::cout << "Minimum length word: " << smallestWord << std::endl;
std::cout << "Maximum length word: " << largestWord << std::endl;
}
int main() {
std::string input = "This is a test string" ;
findSmallestLargestWordsRegex(input);
return 0;
}
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SmallestLargestWords {
public static void findSmallestLargestWordsRegex(String input) {
String wordRegex = "\\b\\w+\\b" ;
Pattern pattern = Pattern.compile(wordRegex);
Matcher matcher = pattern.matcher(input);
String smallestWord = "" ;
String largestWord = "" ;
while (matcher.find()) {
String word = matcher.group();
if (word.length() < smallestWord.length() || smallestWord.isEmpty()) {
smallestWord = word;
}
if (word.length() > largestWord.length()) {
largestWord = word;
}
}
System.out.println( "Minimum length word: " + smallestWord);
System.out.println( "Maximum length word: " + largestWord);
}
public static void main(String[] args) {
String input = "This is a test string" ;
findSmallestLargestWordsRegex(input);
}
}
|
Python3
import re
def find_smallest_largest_words_regex(input_str):
word_regex = r '\b\w+\b'
words = re.findall(word_regex, input_str)
smallest_word = ""
largest_word = ""
for word in words:
if len (word) < len (smallest_word) or not smallest_word:
smallest_word = word
if len (word) > len (largest_word):
largest_word = word
print ( "Minimum length word:" , smallest_word)
print ( "Maximum length word:" , largest_word)
input_str = "This is a test string"
find_smallest_largest_words_regex(input_str)
|
C#
using System;
using System.Text.RegularExpressions;
class Program
{
static void FindSmallestLargestWordsRegex( string input)
{
string wordPattern = @"\b\w+\b" ;
MatchCollection words = Regex.Matches(input, wordPattern);
string smallestWord = null , largestWord = null ;
foreach (Match match in words)
{
string word = match.Value;
if ( string .IsNullOrEmpty(smallestWord) || word.Length < smallestWord.Length)
{
smallestWord = word;
}
if ( string .IsNullOrEmpty(largestWord) || word.Length > largestWord.Length)
{
largestWord = word;
}
}
Console.WriteLine( "Minimum length word: " + smallestWord);
Console.WriteLine( "Maximum length word: " + largestWord);
}
static void Main()
{
string input = "This is a test string" ;
FindSmallestLargestWordsRegex(input);
}
}
|
Javascript
function findSmallestLargestWordsRegex(input) {
const wordRegex = /\b\w+\b/g;
const words = input.match(wordRegex) || [];
let smallestWord = '' , largestWord = '' ;
for (const word of words) {
if (word.length < smallestWord.length || smallestWord.length === 0) {
smallestWord = word;
}
if (word.length > largestWord.length) {
largestWord = word;
}
}
console.log( "Minimum length word:" , smallestWord);
console.log( "Maximum length word:" , largestWord);
}
const input = "This is a test string" ;
findSmallestLargestWordsRegex(input);
|
Output
Minimum length word: a
Maximum length word: string
Time complexity is O(n), n is length of string
Space complexity is O(m), m is the length of the longest word.
Last Updated :
17 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...