具有 k 个不同字符且相邻没有相同字符的字符串
给定 N 和 K,打印一个包含 n 个字符的字符串。字符串应该恰好有 k 个不同的字符,并且没有相邻的位置。
例子:
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.
考虑前 k 个拉丁字母。我们将按顺序将它们添加到答案中,首先,我们添加 a,然后添加 b,依此类推。如果字母已经写完,但答案的长度仍然小于要求的长度,那么我们从字母表的开头重新开始添加字母。我们重复这个过程,直到答案的长度变为 n 并在完成后打印出来。
下面是上述方法的实现
C++
// CPP program to construct a n length string
// with k distinct characters such that no two
// same characters are adjacent.
#include
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;
}
Java
// 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.
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#
// 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.
PHP
Javascript
输出:
ababa