Friday, January 10, 2025
Google search engine
HomeData Modelling & AIFind the String having each substring with exactly K distinct characters

Find the String having each substring with exactly K distinct characters

Given two integers N and K. The task is to find the string of length N, such that each substring of length greater than equal to K, has exactly K distinct characters. 
 

Examples: 

Input: N=10, K=3
Output : ABCABCABCA
Explanation: The output string has 3 distinct characters.

Input : N=20, K=7
Output : ABCDEFGABCDEFGABCDEF
Explanation: The output string has 7 distinct characters.

 

Approach:
To solve the problem mentioned above the main idea is to print distinct elements up to length K, and then repeat the same elements up to N
Below is the implementation of the above approach:
 

C++




// C++ Program to Find the
// String having each substring
// with exactly K distinct characters
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the
// required output string
void findString(int N, int K)
{
    // Each element at index
    // i is modulus of K
    for (int i = 0; i < N; i++) {
 
        cout << char('A' + i % K);
    }
}
 
// Driver code
int main()
{
    // initialise integers N and K
    int N = 10;
    int K = 3;
 
    findString(N, K);
 
    return 0;
}


Java




// Java program to find the
// string having each substring
// with exactly K distinct characters
import java.io.*;
 
class GFG {
 
// Function to find the
// required output string
static void findString(int N, int K)
{
     
    // Each element at index
    // i is modulus of K
    for (int i = 0; i < N; i++)
    {
        System.out.print((char)('A' + i % K));
    }
}
 
// Driver code
public static void main(String[] args)
{
    // Initialise integers N and K
    int N = 10;
    int K = 3;
    findString(N, K);
}
}
 
// This code is contributed by shivanisinghss2110


Python3




# Python3 Program to Find the
# String having each substring
# with exactly K distinct characters
 
# Function to find the
# required output string
def findString(N, K) :
    # Each element at index
    # i is modulus of K
    for i in range(N) :
 
        print(chr(ord('A') + i % K),end="");
 
# Driver code
if __name__ == "__main__" :
    # initialise integers N and K
    N = 10;
    K = 3;
 
    findString(N, K);
     
# This code is contributed by AnkitRai01


C#




// C# program to find the
// string having each substring
// with exactly K distinct characters
using System;
 
class GFG {
 
// Function to find the
// required output string
static void findString(int N, int K)
{
     
    // Each element at index
    // i is modulus of K
    for(int i = 0; i < N; i++)
    {
       Console.Write((char)('A' + i % K));
    }
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Initialise integers N and K
    int N = 10;
    int K = 3;
 
    findString(N, K);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
    // Javascript Program to Find the
    // String having each substring
    // with exactly K distinct characters
     
    // Function to find the
    // required output string
    function findString(N, K)
    {
     
        // Each element at index
        // i is modulus of K
        for (let i = 0; i < N; i++)
        {
            document.write(String.fromCharCode('A'.charCodeAt() + i % K));
        }
    }
     
    // initialise integers N and K
    let N = 10;
    let K = 3;
   
    findString(N, K);
 
// This code is contributed by divyesh072019.
</script>


Output

ABCABCABCA

Time Complexity:O(N)
Auxiliary Space: O(1) 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments