Given a string S. The task is to print the K-th lexicographically the smallest one among the different substrings of s.
A substring of s is a string obtained by taking out a non-empty contiguous part in s.
For example, if s = ababc, a, bab and ababc are substrings of s, while ac, z, and an empty string are not. Also, we say that substrings are different when they are different as strings.
Examples:
Input: str = “aba”, k = 4
Output: b
All unique substrings are a, ab, aba, b, ba.
Thus the 4th lexicographically smallest substring is b.
Input: str = “geeksforgeeks”, k = 5
Output: eeksf
Approach: For an arbitrary string t, each of its proper suffixes is lexicographically smaller than t, and the lexicographic rank of t is at least |t|. Thus, the length of the answer is at most K. Generate all substrings of s whose lengths are at most K. Sort them, unique them, and print the K-th one, where N = |S|.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void kThLexString(string st, int k, int n)
{
set<string> z;
for ( int i = 0; i < n; i++)
{
string pp;
for ( int j = i; j < i + k; j++)
{
if (j >= n)
break ;
pp += st[j];
z.insert(pp);
}
}
vector<string> fin(z.begin(), z.end());
sort(fin.begin(), fin.end());
cout << fin[k - 1];
}
int main()
{
string s = "geeksforgeeks" ;
int k = 5;
int n = s.length();
kThLexString(s, k, n);
}
|
Java
import java.util.*;
class GFG{
public static void kThLexString(String st,
int k, int n)
{
Set<String> z = new HashSet<String>();
for ( int i = 0 ; i < n; i++)
{
String pp = "" ;
for ( int j = i; j < i + k; j++)
{
if (j >= n)
break ;
pp += st.charAt(j);
z.add(pp);
}
}
Vector<String> fin = new Vector<String>();
fin.addAll(z);
Collections.sort(fin);
System.out.print(fin.get(k - 1 ));
}
public static void main(String[] args)
{
String s = "geeksforgeeks" ;
int k = 5 ;
int n = s.length();
kThLexString(s, k, n);
}
}
|
Python3
def kThLexString(st, k, n):
z = set ()
for i in range (n):
pp = ""
for j in range (i, i + k):
if (j > = n):
break
pp + = s[j]
z.add(pp)
fin = list (z)
fin.sort()
print (fin[k - 1 ])
s = "geeksforgeeks"
k = 5
n = len (s)
kThLexString(s, k, n)
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
class GFG{
public static void kThLexString( string st,
int k, int n)
{
HashSet< string > z = new HashSet< string >();
for ( int i = 0; i < n; i++)
{
string pp = "" ;
for ( int j = i; j < i + k; j++)
{
if (j >= n)
break ;
pp += st[j];
z.Add(pp);
}
}
ArrayList fin = new ArrayList();
foreach ( string s in z)
{
fin.Add(s);
}
fin.Sort();
Console.Write(fin[k - 1]);
}
public static void Main( string [] args)
{
string s = "geeksforgeeks" ;
int k = 5;
int n = s.Length;
kThLexString(s, k, n);
}
}
|
Javascript
<script>
function kThLexString(st, k, n)
{
var z = new Set();
for ( var i = 0; i < n; i++)
{
var pp = "" ;
for ( var j = i; j < i + k; j++)
{
if (j >= n)
break ;
pp += st[j];
z.add(pp);
}
}
var fin = [];
z.forEach(element => {
fin.push(element);
});
fin.sort();
document.write( fin[k-1]);
}
var s = "geeksforgeeks" ;
var k = 5;
var n = s.length;
kThLexString(s, k, n);
</script>
|
Time Complexity: O(nk log(nk) where n is the length of the string, and k is the length of the substring.
Space Complexity: O(nk)