Substring lexicográficamente más pequeña y más grande de tamaño k

Dado String str y un entero k, encuentre la substring lexicográficamente más pequeña y más grande de longitud k Orden de
lexicografía, también llamado orden alfabético u orden de diccionario,
 

 A < B <... < Y < Z < a < b <.. < y < z

Ejemplos: 
 

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

Inicializamos max y min como primera substring de tamaño k. Atravesamos las substrings restantes, eliminando el primer carácter de la substring anterior y agregando el último carácter de la nueva string. Realizamos un seguimiento de los lexicográficamente más grandes y más pequeños. 
 

C++

// 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

Java

// 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);
    }
}

Python3

# Python 3 program to find lexicographically
# largest and smallest substrings of size k.
def getSmallestAndLargest(s, k):
     
    # Initialize min and max as
    # first substring of size k
    currStr = s[:k]
    lexMin = currStr
    lexMax = currStr
 
    # Consider all remaining substrings.
    # We consider every substring ending
    # with index i.
    for i in range(k, len(s)):
        currStr = currStr[1 : k] + s[i]
        if (lexMax < currStr):
            lexMax = currStr
        if (lexMin >currStr):
            lexMin = currStr
 
    # Print result.
    print(lexMin)
    print(lexMax)
 
# Driver Code
if __name__ == '__main__':
    str1 = "GeeksForGeeks"
    k = 3
    getSmallestAndLargest(str1, k)
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# program to find lexicographically
// largest and smallest substrings of size k.
using System;
 
class GFG
{
    // Function to compare two strings
    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)
    {
        // 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 - 1) + "" + s[i];
            if (CompareTo(lexMax, currStr) < 0)
                lexMax = currStr;
            if (CompareTo(lexMin, currStr) > 0)
                lexMin = currStr;
        }
 
        // Print result.
        Console.WriteLine(lexMin);
        Console.WriteLine(lexMax);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "GeeksForGeeks";
        int k = 3;
        getSmallestAndLargest(str, k);
    }
}
 
// This code is contributed by
// sanjeev2552

Javascript

<script>
 
      // JavaScript program to find lexicographically
      // largest and smallest substrings of size k.
 
      function getSmallestAndLargest(s, k) {
        // Initialize min and max as
        // first substring of size k
        var currStr = s.substring(0, k);
        var lexMin = currStr;
        var lexMax = currStr;
 
        // Consider all remaining substrings.
        // We consider every substring
        // ending with index i.
        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;
        }
 
        // Print result.
        document.write(lexMin + "<br>");
        document.write(lexMax + "<br>");
      }
 
      // Driver Code
      var str = "GeeksForGeeks";
      var k = 3;
      getSmallestAndLargest(str, k);
       
</script>
Producción: 

For
sFo

 

Publicación traducida automáticamente

Artículo escrito por bilal-hungund y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *