使用所有字母表的前 K 个字母且没有两个相邻字符相同的字典最小字符串
给定两个整数N和K ,任务是按照给定条件使用字母表的前K个字符形成大小为N的字符串:
- 字符串中没有两个相邻的字符是相同的。
- 所有K个字符在字符串中至少出现一次。
如果不可能有这样的字符串,则打印 -1。
例子:
Input: N = 3, K = 2
Output: “aba”
Explanation: This is the lexicographically smallest string which follows the condition.
“aaa” is lexicographically smallest string of size 3, but this does not contain ‘b’.
So it is not a valid string according to the statement.
“aab” is also lexicographically smaller, but it violates the condition of two adjacent characters not being the same.
Input: N = 2, K = 3
Output: -1
Explanation: Have to choose first 3 characters, but a string of only 2 size should be formed.
So it is not possible to use all of the first 3 characters.
方法:这是一个基于实现的问题。众所周知,如果在字符串,则字符串在字典上会更小。请按照以下步骤操作:
- 如果N < K或K = 1 且 N > 1则打印'-1'以形成满足这两个条件的字符串是不可能的。
- 否则,如果N = 2则打印“ab” 。
- 如果N > 2则在结果字符串中交替添加'a'和'b' ,直到剩余长度为K-2 。然后按字典顺序添加除“a”和“b”之外的其余字符。
- 最后一个字符串是必需的字符串。
下面是上述方法的实现。
C++
// C++ program to implement the approach
#include
using namespace std;
// Function to find the
// lexicographically smallest string
string findmin(int N, int K)
{
// If size of given string is
// more than first K letters of
// alphabet then print -1.
// If K = 1 and N > 1 then it
// violates the rule that
// adjacent characters should be different
if (N < K or (K == 1 and N > 1))
return "-1";
// If N = 2 then print "ab"
if (N == 2)
return "ab";
string s;
// Except "ab" add characters
// in the string
for (int i = 2; i < K; i++) {
s += char(i + 97);
}
string a = "ab", b;
int i = 0;
while (i < N) {
// Add 'a' and 'b' alternatively
b += a[i % 2];
i++;
// If there are other characters
// than 'a' and 'b'
if (N - i == K - 2) {
for (int i = 0; i < s.size();
i++) {
b += s[i];
}
break;
}
}
// Desired string
return b;
}
// Driver code
int main()
{
int N = 3, K = 2;
// Function call
cout << findmin(N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the
// lexicographically smallest string
static String findmin(int N, int K)
{
// If size of given string is
// more than first K letters of
// alphabet then print -1.
// If K = 1 and N > 1 then it
// violates the rule that
// adjacent characters should be different
if (N < K || (K == 1 && N > 1))
return "-1";
// If N = 2 then print "ab"
if (N == 2)
return "ab";
String s = "";
// Except "ab" add characters
// in the string
for (int i = 2; i < K; i++) {
char ch = (char)(i + 97);
s += ch;
}
String a = "ab", b = "";
int i = 0;
while (i < N) {
// Add 'a' and 'b' alternatively
b += a.charAt(i % 2);
i++;
// If there are other characters
// than 'a' and 'b'
if (N - i == K - 2) {
for (int j = 0; j < s.length();j++) {
b += s.charAt(j);
}
break;
}
}
// Desired string
return b;
}
// Driver code
public static void main (String[] args) {
int N = 3, K = 2;
System.out.println(findmin(N, K));
}
}
// This code is contributed by hrithikgarg03188.
Python
# Python program to implement the approach
# Function to find the
# lexicographically smallest string
def findmin(N, K):
# If size of given string is
# more than first K letters of
# alphabet then print -1.
# If K = 1 and N > 1 then it
# violates the rule that
# adjacent characters should be different
if (N < K or (K == 1 and N > 1)):
return "-1"
# If N = 2 then print "ab"
if (N == 2):
return "ab"
s = ""
# Except "ab" add characters
# in the string
for i in range(2, K):
s += (i + 97)
a = "ab"
b = ""
i = 0
while (i < N):
# Add 'a' and 'b' alternatively
b += a[i % 2]
i += 1
# If there are other characters
# than 'a' and 'b'
if (N - i == K - 2):
for j in range(len(s)):
b += s[i]
break
# Desired string
return b
# Driver code
N = 3
K = 2
# Function call
print(findmin(N, K))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program to implement the approach
using System;
class GFG
{
// Function to find the
// lexicographically smallest string
static string findmin(int N, int K)
{
// If size of given string is
// more than first K letters of
// alphabet then print -1.
// If K = 1 and N > 1 then it
// violates the rule that
// adjacent characters should be different
if (N < K || (K == 1 && N > 1))
return "-1";
// If N = 2 then print "ab"
if (N == 2)
return "ab";
string s = "";
// Except "ab" add characters
// in the string
for (int x = 2; x < K; x++) {
s += (char)(x + 97);
}
string a = "ab", b = "";
int i = 0;
while (i < N) {
// Add 'a' and 'b' alternatively
b += a[i % 2];
i++;
// If there are other characters
// than 'a' and 'b'
if (N - i == K - 2) {
for (int j = 0; j < s.Length;
j++) {
b += s[j];
}
break;
}
}
// Desired string
return b;
}
// Driver code
public static void Main()
{
int N = 3, K = 2;
// Function call
Console.Write(findmin(N, K));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
aba
时间复杂度: O(N)
辅助空间: O(N)