Given two integers n and k returns a palindrome of length n which consists of k distinct lower case

10/26/2019String with k distinct characters and no same characters adjacent - GeeksforGeeks1/9String with k distinct characters and no same char-acters adjacentGiven N and K, print a string that has n characters. The string should have exactly k dis-tinct characters and no adjacent positions.Examples:Input: n = 5, k = 3Output :abcabExplanation: 3 distinct character a, b, cand n length string.Input: 3 2Output: abaExplanation: 2 distinct character 'a'and 'b' and n length string.Recommended: Please try your approach on{IDE}±rst, before moving on tothe solution.Consider the ±rst k Latin letters. We will add them to the answer in the order, ±rstly, weadd a, then b and so on. If letters are ±nished but the length of the answer is still less thanthe 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.Custom SearchCOURSESHIRE WITH USLogin

Count of palindromes of length N having atmost k distinct characters such that no prefix (of size 2 to n-1) is palindrome.

for example If N = 3 and K = 3, possible palindromes are "aba", "aca", "bab", "bcb", "cac" and "cbc". so count is 6.

Here is a link of problem and solution https://www.hackerearth.com/problem/algorithm/avoid-prefix-palindromes-cdd47bd7-780d0bca/editorial/

Can anyone help me with this problem?.

Not able to understand the solution mentioned on hackerearth as there is no editorial there. Having difficult time understanding how states are defined in solution.

Improve Article

Save Article

Like Article

  • Read
  • Discuss
  • Improve Article

    Save Article

    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 ones, 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:

    #include <iostream>

    using namespace std;

    string findString(int n, int k)

    {

        string res = "";

        for (int i = 0; i < k; i++)

            res = res + (char)('a' + i);

        int count = 0;

        for (int i = 0; i < n - k; i++) {

            res = res + (char)('a' + count);

            count++;

            if (count == k)

                count = 0;

        }

        return res;

    }

    int main()

    {

        int n = 5, k = 2;

        cout << findString(n, k);

        return 0;

    }

    import java.io.*;

    public class GFG {

        static String findString(int n, int k)

        {

            String res = "";

            for (int i = 0; i < k; i++)

                res = res + (char)('a' + i);

            int count = 0;

            for (int i = 0; i < n - k; i++)

            {

                res = res + (char)('a' + count);

                count++;

                if (count == k)

                    count = 0;

            }

            return res;

        }

        static public void main (String[] args)

        {

            int n = 5, k = 2;

            System.out.println(findString(n, k));

        }

    }

    def findString(n, k):

        res = ""

        for i in range(k):

            res = res + chr(ord('a') + i)

        count = 0

        for i in range(n - k) :

            res = res + chr(ord('a') + count)

            count += 1

            if (count == k):

                count = 0;

        return res

    if __name__ == "__main__":

        n = 5

        k = 2

        print(findString(n, k))

    using System;

    public class GFG {

        static string findString(int n, int k)

        {

            string res = "";

            for (int i = 0; i < k; i++)

                res = res + (char)('a' + i);

            int count = 0;

            for (int i = 0; i < n - k; i++)

            {

                res = res + (char)('a' + count);

                count++;

                if (count == k)

                    count = 0;

            }

            return res;

        }

        static public void Main ()

        {

            int n = 5, k = 2;

            Console.WriteLine(findString(n, k));

        }

    }

    <?php

    function findString($n, $k)

    {

        $res = "";

        for ($i = 0; $i < $k; $i++)

            $res = $res . chr(ord('a') + $i);

        $count = 0;

        for ($i = 0; $i < $n - $k; $i++)

        {

            $res = $res . chr(ord('a')

                              + $count);

            $count++;

            if ($count == $k)

                $count = 0;

        }

        return $res;

    }

        $n = 5;

        $k = 2;

        echo findString($n, $k);

    ?>

    <script>

        function findString(n, k)

        {

            let res = "";

            for (let i = 0; i < k; i++)

                res = res + String.fromCharCode('a'.charCodeAt(0) + i);

            let count = 0;

            for (let i = 0; i < n - k; i++)

            {

                res = res + String.fromCharCode('a'.charCodeAt(0) + count);

                count++;

                if (count == k)

                    count = 0;

            }

            return res;

        }

         let n = 5, k = 2;

         document.write(findString(n, k));

    </script>

    Time complexity : O(n), where n is the given integer.
    Auxiliary Space: O(n), where n is the given integer.

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