Genere una string de tamaño N cuya substring de tamaño M tenga exactamente K caracteres distintos

Dados 3 números enteros positivos N , M y K . la tarea es construir una string de longitud N que consista en letras minúsculas de modo que cada substring de longitud M tenga exactamente K letras distintas.
Ejemplos: 

Entrada: N = 5, M = 2, K = 2 
Salida: abade 
Explicación: 
Cada substring de tamaño 2 «ab», «ba», «ad», «de» tiene 2 letras distintas.
Entrada: N = 7, M = 5, K = 3 
Salida: abcaaab 
Explicación: 
Cada substring de tamaño 5 «tleel», «leelt», «eelte» tiene 3 letras distintas. 
 

Enfoque: en una string de tamaño N, cada substring de tamaño M debe contener exactamente K letras distintas. 

  • Construya una string que tenga K alfabetos distintos comenzando desde ‘a’ hasta ‘z’ hasta el tamaño de M y coloque el resto de letras como ‘a’.
  • Ya que hemos generado una string de tamaño M con K valor distinto. Ahora, sigue repitiéndolo hasta que alcancemos un tamaño de string de N.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to generate a string of size N
// whose each substring of size M
// has exactly K distinct characters
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to generate the string
string generateString(int N, int M, int K)
{
 
    // Declare empty string
    string s = "";
 
    // counter for M
    int cnt1 = 0;
 
    // counter for K
    int cnt2 = 0;
 
    // Loop to generate string size of N
    for (int i = 0; i < N; i++) {
        cnt1++;
        cnt2++;
 
        // Generating K distinct
        // letters one by one
        if (cnt1 <= M) {
            if (cnt2 <= K) {
                s = s + char(96 + cnt1);
            }
 
            // After generating b distinct letters,
            // append rest a-b letters as 'a'
            else {
                s = s + 'a';
            }
        }
 
        // Reset the counter value
        // and repeat the process
        else {
            cnt1 = 1;
            cnt2 = 1;
            s = s + 'a';
        }
    }
 
    // return final result string
    return s;
}
 
// Driver code
int main()
{
    int N = 7, M = 5, K = 3;
 
    cout << generateString(N, M, K) << endl;
 
    return 0;
}

Java

// Java program to generate a String of size N
// whose each subString of size M
// has exactly K distinct characters
import java.util.*;
class GFG{
 
// Function to generate the String
static String generateString(int N, int M, int K)
{
 
    // Declare empty String
    String s = "";
 
    // counter for M
    int cnt1 = 0;
 
    // counter for K
    int cnt2 = 0;
 
    // Loop to generate String size of N
    for (int i = 0; i < N; i++)
    {
        cnt1++;
        cnt2++;
 
        // Generating K distinct
        // letters one by one
        if (cnt1 <= M)
        {
            if (cnt2 <= K)
            {
                s = s + (char)(96 + cnt1);
            }
 
            // After generating b distinct letters,
            // append rest a-b letters as 'a'
            else
            {
                s = s + 'a';
            }
        }
 
        // Reset the counter value
        // and repeat the process
        else
        {
            cnt1 = 1;
            cnt2 = 1;
            s = s + 'a';
        }
    }
 
    // return final result String
    return s;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 7, M = 5, K = 3;
 
    System.out.println(generateString(N, M, K));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to generate a string of size N
# whose each substring of size M
# has exactly K distinct characters
# Function to generate the string
def generateString(N, M, K):
 
    # Declare empty string
    s = ""
 
    # counter for M
    cnt1 = 0
 
    # counter for K
    cnt2 = 0
 
    # Loop to generate string size of N
    for i in range (N):
        cnt1 += 1
        cnt2 += 1
 
        # Generating K distinct
        # letters one by one
        if (cnt1 <= M):
            if (cnt2 <= K):
                s = s + chr(96 + cnt1)
             
            # After generating b distinct letters,
            # append rest a-b letters as 'a'
            else:
                s = s + 'a'
 
        # Reset the counter value
        # and repeat the process
        else:
            cnt1 = 1
            cnt2 = 1
            s = s + 'a'
 
    # return final result string
    return s
 
# Driver code
if __name__ == "__main__": 
    N = 7
    M = 5
    K = 3
    print (generateString(N, M, K))
     
# This code is contributed by Chitranayal

C#

// C# program to generate a String of
// size N whose each subString of size
// M has exactly K distinct characters
using System;
 
class GFG{
 
// Function to generate the String
static String generateString(int N, int M, int K)
{
 
    // Declare empty String
    String s = "";
 
    // Counter for M
    int cnt1 = 0;
 
    // Counter for K
    int cnt2 = 0;
 
    // Loop to generate String size of N
    for(int i = 0; i < N; i++)
    {
       cnt1++;
       cnt2++;
        
       // Generating K distinct
       // letters one by one
       if (cnt1 <= M)
       {
           if (cnt2 <= K)
           {
               s = s + (char)(96 + cnt1);
           }
            
           // After generating b distinct letters,
           // append rest a-b letters as 'a'
           else
           {
               s = s + 'a';
           }
       }
        
       // Reset the counter value
       // and repeat the process
       else
       {
           cnt1 = 1;
           cnt2 = 1;
           s = s + 'a';
       }
    }
     
    // Return readonly result String
    return s;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 7, M = 5, K = 3;
 
    Console.WriteLine(generateString(N, M, K));
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
 
// Javascript program to generate
// a String of size N
// whose each subString of size M
// has exactly K distinct characters
 
    // Function to generate the String
    function generateString(N,M,K)
    {
        // Declare empty string
    let s = "";
  
    // counter for M
    let cnt1 = 0;
  
    // counter for K
    let cnt2 = 0;
  
    // Loop to generate string size of N
    for (let i = 0; i < N; i++) {
        cnt1++;
        cnt2++;
  
        // Generating K distinct
        // letters one by one
        if (cnt1 <= M) {
            if (cnt2 <= K) {
                s = s + String.fromCharCode(96 + cnt1);
            }
  
            // After generating b distinct letters,
            // append rest a-b letters as 'a'
            else {
                s = s + 'a';
            }
        }
  
        // Reset the counter value
        // and repeat the process
        else {
            cnt1 = 1;
            cnt2 = 1;
            s = s + 'a';
        }
    }
  
    // return final result string
    return s;
    }
     
    // Driver code
    let N = 7, M = 5, K = 3;
    document.write( generateString(N, M, K))
     
     
    // This code is contributed
    // by avanitrachhadiya2155
     
</script>
Producción: 

abcaaab

 

Complejidad temporal: O(N) 
Complejidad espacial: O(1)

Publicación traducida automáticamente

Artículo escrito por divyeshrabadiya07 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 *