String with k distinct characters and no same characters adjacent

Given N and K, print a string that has n characters. The string should have exactly k distinct characters and no adjacent positions.

Examples:

Input  : n = 5, k = 3 
Output :  abcab
Explanation: 3 distinct character a, b, c
and n length string. 

Input: 3 2
Output: aba 
Explanation: 2 distinct character 'a' 
and 'b' and n length string.

Consider the first k Latin letters. We will add them to the answer in the order, firstly, we add a, then b and so on. If letters are finished but the length of the answer is still less than the required one, then we start again adding letters from the beginning of the alphabet. We repeat this process until the length of the answer becomes n and print it once done.

Below is the implementation of the above approach

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to construct a n length string
// with k distinct characters such that no two
// same characters are adjacent.
#include <iostream>
using namespace std;
  
// Function to find a string of length
// n with k distinct characters.
string findString(int n, int k)
{
    // Initialize result with first k
    // Latin letters
    string res = "";
    for (int i = 0; i < k; i++)
        res = res + (char)('a' + i);
  
    // Fill remaining n-k letters by
    // repeating k letters again and again.
    int count = 0;
    for (int i = 0; i < n - k; i++) {
        res = res + (char)('a' + count);
        count++;
        if (count == k)
            count = 0;
    }
    return res;
}
  
// Driver code
int main()
{
    int n = 5, k = 2;
    cout << findString(n, k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to construct a n length
// string with k distinct characters
// such that no two same characters
// are adjacent.
import java.io.*;
  
public class GFG {
      
    // Function to find a string of
    // length n with k distinct characters.
    static String findString(int n, int k)
    {
          
        // Initialize result with first k
        // Latin letters
        String res = "";
          
        for (int i = 0; i < k; i++)
            res = res + (char)('a' + i);
      
        // Fill remaining n-k letters by
        // repeating k letters again and 
        // again.
        int count = 0;
          
        for (int i = 0; i < n - k; i++)
        {
            res = res + (char)('a' + count);
            count++;
              
            if (count == k)
                count = 0;
        }
          
        return res;
    }
      
    // Driver code
    static public void main (String[] args)
    {
      
        int n = 5, k = 2;
          
        System.out.println(findString(n, k));
    }
}
  
// This article is contributed by vt_m.

chevron_right


Python 3

# Python 3 program to construct a n
# length string with k distinct characters
# such that no two same characters are adjacent.

# Function to find a string of length
# n with k distinct characters.
def findString(n, k):

# Initialize result with first k
# Latin letters
res = “”
for i in range(k):
res = res + chr(ord(‘a’) + i)

# Fill remaining n-k letters by
# repeating k letters again and again.
count = 0
for i in range(n – k) :
res = res + chr(ord(‘a’) + count)
count += 1
if (count == k):
count = 0;

return res

# Driver code
if __name__ == “__main__”:

n = 5
k = 2
print(findString(n, k))

# This code is contributed by ita_c

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to construct a n length
// string with k distinct characters
// such that no two same characters
// are adjacent.
using System;
  
public class GFG {
      
    // Function to find a string
    // of length n with k distinct
    // characters.
    static string findString(int n, int k)
    {
          
        // Initialize result with
        // first k Latin letters
        string res = "";
          
        for (int i = 0; i < k; i++)
            res = res + (char)('a' + i);
      
        // Fill remaining n-k letters by
        // repeating k letters again and
        // again.
        int count = 0;
          
        for (int i = 0; i < n - k; i++)
        {
            res = res + (char)('a' + count);
            count++;
              
            if (count == k)
                count = 0;
        }
          
        return res;
    }
      
    // Driver code
    static public void Main ()
    {
          
        int n = 5, k = 2;
          
        Console.WriteLine(findString(n, k));
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// php program to construct a n length
// string with k distinct characters 
// such that no two same characters 
// are adjacent.
  
// Function to find a string of length
// n with k distinct characters.
function findString($n, $k)
{
    // Initialize result with first k
    // Latin letters
    $res = "";
    for ($i = 0; $i < $k; $i++)
        $res = $res . chr(ord('a') + $i);
  
    // Fill remaining n-k letters by
    // repeating k letters again and
    // again.
    $count = 0;
    for ($i = 0; $i < $n - $k; $i++) 
    {
        $res = $res . chr(ord('a'
                          + $count);
        $count++;
        if ($count == $k)
            $count = 0;
    }
    return $res;
}
  
// Driver code
    $n = 5;
    $k = 2;
      
    echo findString($n, $k);
          
// This code is contributed by Sam007
?>

chevron_right



Output:

ababa

Time complexity : O(n)

This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : vt_m, Sam007, iamchinu97, Ita_c



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.