K-th lexicographical string of given length
Last Updated :
05 Aug, 2021
Given two integers N and K, the task is to find lexicographically Kth string of length N. If the number of possible strings of length N is less than K, print -1.
Examples:
Input: N = 3, K = 10
Output: “aaj”
Explanation: The 10th string in the lexicographical order starting from “aaa” is “aaj”.
Input: N = 2, K = 1000
Output: -1
Explanation: A total of 26*26 = 676 strings of length 2 are possible. So the output will be -1.
Approach:
- Let us assume a string of length N as an integer of base 26.
- For example, if N = 3, the first string will be s = “aaa” whose base 26 representation in integer will be 0 0 0, the second string will be s = “aab” and base 26 representation will be 0 0 1 and so on. Hence each digit can have a value within [0, 25].
- Starting from the last digit, we need to change it to all possible values followed by the penultimate digit and so on.
- So, the simplified problem is to find the representation of a decimal number K into a 26 base number. You can read this post to have a clear idea how to do this.
- After we find the answer in the form of an integer of base 26, we will convert each digit into its equivalent character, where 0 is ‘a’, 1 is ‘b’, …. 24 is ‘y’, 25 is ‘z’.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string find_kth_String_of_n( int n, int k)
{
int d[n] = {0};
for ( int i = n - 1; i > -1; i--)
{
d[i] = k % 26;
k /= 26;
}
if (k > 0)
return "-1" ;
string s = "" ;
for ( int i = 0; i < n; i++)
s += (d[i] + ( 'a' ));
return s;
}
int main()
{
int n = 3;
int k = 10;
k -= 1;
cout << find_kth_String_of_n(n, k);
return 0;
}
|
Java
class GFG{
static String find_kth_String_of_n( int n, int k)
{
int [] d = new int [n];
for ( int i = n - 1 ; i > - 1 ; i--)
{
d[i] = k % 26 ;
k /= 26 ;
}
if (k > 0 )
return "-1" ;
String s = "" ;
for ( int i = 0 ; i < n; i++)
s += ( char )(d[i] + ( 'a' ));
return s;
}
public static void main(String[] args)
{
int n = 3 ;
int k = 10 ;
k -= 1 ;
System.out.println(find_kth_String_of_n(n, k));
}
}
|
Python3
def find_kth_string_of_n(n, k):
d = [ 0 for i in range (n)]
for i in range (n - 1 , - 1 , - 1 ):
d[i] = k % 26
k / / = 26
if k> 0 :
return - 1
s = ""
for i in range (n):
s + = chr (d[i] + ord ( 'a' ))
return s
n = 3
k = 10
k - = 1
print (find_kth_string_of_n(n, k))
|
C#
using System;
class GFG{
static String find_kth_String_of_n( int n, int k)
{
int [] d = new int [n];
for ( int i = n - 1; i > -1; i--)
{
d[i] = k % 26;
k /= 26;
}
if (k > 0)
return "-1" ;
string s = "" ;
for ( int i = 0; i < n; i++)
s += ( char )(d[i] + ( 'a' ));
return s;
}
public static void Main()
{
int n = 3;
int k = 10;
k -= 1;
Console.Write(find_kth_String_of_n(n, k));
}
}
|
Javascript
<script>
function find_kth_String_of_n(n, k)
{
let d = new Array(n);
d.fill(0);
for (let i = n - 1; i > -1; i--)
{
d[i] = k % 26;
k = parseInt(k / 26, 10);
}
if (k > 0)
return "-1" ;
let s = "" ;
for (let i = 0; i < n; i++)
s += String.fromCharCode(d[i] + ('a').charCodeAt());
return s;
}
let n = 3;
let k = 10;
k -= 1;
document.write(find_kth_String_of_n(n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...