Given String str and an integer k, find the lexicographically smallest and largest substring of length k
Lexicography order, also called as alphabetical order or dictionary order,
A < B <... < Y < Z < a < b <.. < y < z
Examples:
Input : String: hello
Size: 2
Distinct Substring: [el, he, ll, lo]
Output : Smallest Substring: el
Largest Substring: lo
Input : String: geeksforgeeks
Size: 3
Distinct Substring: [eek, eks, for, gee, ksf, org, rge, sfo]
Output : Smallest Substring: eek
Largest Substring: sfo
We initialize max and min as the first substring of size k. We traverse the remaining substrings, by removing the first character of the previous substring and adding the last character of the new string. We keep track of the lexicographically largest and smallest.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void getSmallestAndLargest(string s, int k)
{
string currStr = s.substr(0, k);
string lexMin = currStr;
string lexMax = currStr;
for ( int i = k; i < s.length(); i++)
{
currStr = currStr.substr(1, k) + s[i];
if (lexMax < currStr)
lexMax = currStr;
if (lexMin >currStr)
lexMin = currStr;
}
cout << (lexMin) << endl;
cout << (lexMax) << endl;
}
int main()
{
string str = "GeeksForGeeks" ;
int k = 3;
getSmallestAndLargest(str, k);
}
|
Java
public class GFG {
public static void getSmallestAndLargest(String s, int k)
{
String currStr = s.substring( 0 , k);
String lexMin = currStr;
String lexMax = currStr;
for ( int i = k; i < s.length(); i++) {
currStr = currStr.substring( 1 , k) + s.charAt(i);
if (lexMax.compareTo(currStr) < 0 )
lexMax = currStr;
if (lexMin.compareTo(currStr) > 0 )
lexMin = currStr;
}
System.out.println(lexMin);
System.out.println(lexMax);
}
public static void main(String[] args)
{
String str = "GeeksForGeeks" ;
int k = 3 ;
getSmallestAndLargest(str, k);
}
}
|
Python3
def getSmallestAndLargest(s, k):
currStr = s[:k]
lexMin = currStr
lexMax = currStr
for i in range (k, len (s)):
currStr = currStr[ 1 : k] + s[i]
if (lexMax < currStr):
lexMax = currStr
if (lexMin >currStr):
lexMin = currStr
print (lexMin)
print (lexMax)
if __name__ = = '__main__' :
str1 = "GeeksForGeeks"
k = 3
getSmallestAndLargest(str1, k)
|
C#
using System;
class GFG
{
static int CompareTo(String s1, String s2)
{
for ( int i = 0; i < s1.Length ||
i < s2.Length; i++)
{
if (s1[i] < s2[i])
return -1;
else if (s1[i] > s2[i])
return 1;
}
return 0;
}
static void getSmallestAndLargest(String s, int k)
{
String currStr = s.Substring(0, k);
String lexMin = currStr;
String lexMax = currStr;
for ( int i = k; i < s.Length; i++)
{
currStr = currStr.Substring(1, k - 1) + "" + s[i];
if (CompareTo(lexMax, currStr) < 0)
lexMax = currStr;
if (CompareTo(lexMin, currStr) > 0)
lexMin = currStr;
}
Console.WriteLine(lexMin);
Console.WriteLine(lexMax);
}
public static void Main(String[] args)
{
String str = "GeeksForGeeks" ;
int k = 3;
getSmallestAndLargest(str, k);
}
}
|
Javascript
<script>
function getSmallestAndLargest(s, k) {
var currStr = s.substring(0, k);
var lexMin = currStr;
var lexMax = currStr;
for ( var i = k; i < s.length; i++) {
currStr = currStr.substring(1, k) + s[i];
if (lexMax < currStr) lexMax = currStr;
if (lexMin > currStr) lexMin = currStr;
}
document.write(lexMin + "<br>" );
document.write(lexMax + "<br>" );
}
var str = "GeeksForGeeks" ;
var k = 3;
getSmallestAndLargest(str, k);
</script>
|
Time Complexity:- The time complexity of this program is O(n*k), where n is the length of the input string s and k is the size of the substring.
Auxiliary Space:- The auxiliary space used by this program is O(k), which is the space required to store the current substring, the lexicographically smallest substring, and the lexicographically largest substring. This space usage does not depend on the length of the input string, so it is considered constant space.