Given N and K, print a string that has n characters. The string should have exactly k distinct characters and no adjacent positions.
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
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 firstname.lastname@example.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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimize length of string by replacing K pairs of distinct adjacent characters
- Rearrange characters in a string such that no two adjacent are same
- Rearrange characters in a string such that no two adjacent are same using hashing
- Rearrange string such that no pair of adjacent characters are of the same type
- Replace '?' in a string such that no two adjacent characters are same
- Minimum string such that every adjacent character of given string is still adjacent
- Find distinct characters in distinct substrings of a string
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Smallest length string with repeated replacement of two distinct adjacent
- Min flips of continuous characters to make all characters same in a string
- Check whether two strings contain same characters in same order
- Make the string lexicographically smallest and non palindromic by swapping of adjacent pair of characters
- Replace minimal number of characters to make all characters pair wise distinct
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimum characters to be replaced to make frequency of all characters same
- Minimum replacements to make adjacent characters unequal in a ternary string