Lexicographically smallest String using all of the first K letters of alphabet and no two adjacent characters are same
Given two integers N and K, the task is to form a string of size N using the first K characters of the alphabet following the given conditions:
- No two adjacent characters of the string are the same.
- All of the K characters are present at least once in the string.
If no such string is possible, print -1.
Examples:
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.
Approach: This is an implementation based problem. As known, if the starting characters of the alphabet are used at the starting of the string, then the string will be lexicographically smaller. Follow the steps mentioned below:
- if N < K or K = 1 and N > 1 then print ‘-1’ as forming a string that satisfies both the condition is not possible.
- Otherwise, if N = 2 then print “ab”.
- If N > 2 then add ‘a’ and ‘b’ in the resultant string alternatively until the remaining length is K-2. Then add the remaining characters except ‘a’ and ‘b’ in lexicographical order.
- The final string is the required string.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string findmin( int N, int K)
{
if (N < K or (K == 1 and N > 1))
return "-1" ;
if (N == 2)
return "ab" ;
string s;
for ( int i = 2; i < K; i++) {
s += char (i + 97);
}
string a = "ab" , b;
int i = 0;
while (i < N) {
b += a[i % 2];
i++;
if (N - i == K - 2) {
for ( int i = 0; i < s.size();
i++) {
b += s[i];
}
break ;
}
}
return b;
}
int main()
{
int N = 3, K = 2;
cout << findmin(N, K);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static String findmin( int N, int K)
{
if (N < K || (K == 1 && N > 1 ))
return "-1" ;
if (N == 2 )
return "ab" ;
String s = "" ;
for ( int i = 2 ; i < K; i++) {
char ch = ( char )(i + 97 );
s += ch;
}
String a = "ab" , b = "" ;
int i = 0 ;
while (i < N) {
b += a.charAt(i % 2 );
i++;
if (N - i == K - 2 ) {
for ( int j = 0 ; j < s.length();j++) {
b += s.charAt(j);
}
break ;
}
}
return b;
}
public static void main (String[] args) {
int N = 3 , K = 2 ;
System.out.println(findmin(N, K));
}
}
|
Python
def findmin(N, K):
if (N < K or (K = = 1 and N > 1 )):
return "-1"
if (N = = 2 ):
return "ab"
s = ""
for i in range ( 2 , K):
s + = (i + 97 )
a = "ab"
b = ""
i = 0
while (i < N):
b + = a[i % 2 ]
i + = 1
if (N - i = = K - 2 ):
for j in range ( len (s)):
b + = s[i]
break
return b
N = 3
K = 2
print (findmin(N, K))
|
C#
using System;
class GFG
{
static string findmin( int N, int K)
{
if (N < K || (K == 1 && N > 1))
return "-1" ;
if (N == 2)
return "ab" ;
string s = "" ;
for ( int x = 2; x < K; x++) {
s += ( char )(x + 97);
}
string a = "ab" , b = "" ;
int i = 0;
while (i < N) {
b += a[i % 2];
i++;
if (N - i == K - 2) {
for ( int j = 0; j < s.Length;
j++) {
b += s[j];
}
break ;
}
}
return b;
}
public static void Main()
{
int N = 3, K = 2;
Console.Write(findmin(N, K));
}
}
|
Javascript
<script>
const findmin = (N, K) => {
if (N < K || (K == 1 && N > 1))
return "-1" ;
if (N == 2)
return "ab" ;
let s = "" ;
for (let i = 2; i < K; i++) {
s += String.fromCharCode(i + 97);
}
let a = "ab" , b = "" ;
let i = 0;
while (i < N) {
b += a[i % 2];
i++;
if (N - i == K - 2) {
for (let i = 0; i < s.length; i++) {
b += s[i];
}
break ;
}
}
return b;
}
let N = 3, K = 2;
document.write(findmin(N, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
22 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...