📌  相关文章
📜  产生的大小为N,其大小为M的各子串具有正好k个不同字符的字符串

📅  最后修改于: 2021-04-22 08:53:24             🧑  作者: Mango

给定3个正整数NMK。任务是构造的长度N由小写字母,使得具有正好k个不同字母长度M的每个子字符串。
例子:

方法:在大小为N的字符串,尺寸的每串中号必须包含正好k个不同letters-

  • 构造一个具有从’a’到’z’直至M大小的K个不同字母的字符串,并将其余的字母(如’a’)放入。
  • 由于我们生成了一个大小为M的字符串,具有K个不同的值。现在,继续重复直到达到N的字符串大小。

下面是上述方法的实现:

C++
// C++ program to generate a string of size N
// whose each substring of size M
// has exactly K distinct characters
 
#include 
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


输出:
abcaaab



时间复杂度: O(N)
空间复杂度: O(1)