Lexicographically smallest and largest substring of size k

Given a 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 first substring of size k. We traverse remaining substrings, by removing first character of previous substring and adding last character of new string. We keep track of the lexicographically largest and smallest.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find lexicographically 
// largest and smallest substrings of size k.
#include<bits/stdc++.h>
  
using namespace std;
  
    void getSmallestAndLargest(string s, int k)
    {
          
        // Initialize min and max as 
        // first substring of size k
        string currStr = s.substr(0, k);
        string lexMin = currStr;
        string lexMax = currStr;
  
        // Consider all remaining substrings. We consider
        // every substring ending with index i.
        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;    
        }
  
        // Print result.
        cout << (lexMin) << endl;
        cout << (lexMax) << endl;
    }
  
    // Driver Code
    int main()
    {
        string str = "GeeksForGeeks";
        int k = 3;
        getSmallestAndLargest(str, k);
    }
  
// This code is contributed by
// Sanjit_Prasad

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find lexicographically largest and smallest
// substrings of size k.
  
public class GFG {
  
    public static void getSmallestAndLargest(String s, int k)
    {
        // Initialize min and max as first substring of size k
        String currStr = s.substring(0, k);
        String lexMin = currStr;
        String lexMax = currStr;
   
        // Consider all remaining substrings. We consider
        // every substring ending with index i.
        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;            
        }
  
        // Print result.
        System.out.println(lexMin);
        System.out.println(lexMax);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String str = "GeeksForGeeks";
        int k = 3;
        getSmallestAndLargest(str, k);
    }
}

chevron_right


Output:

For
sFo


My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.