Queries to answer the X-th smallest sub-string lexicographically
Given a string str and Q queries. Every query consists of a number X, the task is to print the Xth lexicographically smallest sub-string of the given string str.
Examples:
Input: str = “geek”, q[] = {1, 5, 10}
Output:
e
ek
k
“e”, “e”, “ee”, “eek”, “ek”, “g”, “ge”, “gee”, “geek” and “k” are
all the possible sub-strings in lexicographically sorted order.
Input: str = “abcgdhge”, q[] = {15, 32}
Output:
bcgdhge
gdhge
Approach: Generate all the sub-strings and store them in any data structure and sort that data structure lexicographically. In the solution below, we have used a vector to store all the sub-strings and the inbuilt sort function sorts them in the given order. Now, for every query print vec[X – 1], which will be the Xth smallest sub-string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void pre_process(vector<string>& substrings, string s)
{
int n = s.size();
for ( int i = 0; i < n; i++) {
string dup = "" ;
for ( int j = i; j < n; j++) {
dup += s[j];
substrings.push_back(dup);
}
}
sort(substrings.begin(), substrings.end());
}
int main()
{
string s = "geek" ;
vector<string> substrings;
pre_process(substrings, s);
int queries[] = { 1, 5, 10 };
int q = sizeof (queries) / sizeof (queries[0]);
for ( int i = 0; i < q; i++)
cout << substrings[queries[i] - 1] << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void pre_process(String substrings[],String s)
{
int n = s.length();
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
String dup = "" ;
for ( int j = i; j < n; j++)
{
dup += s.charAt(j);
substrings[count++] = dup;
}
}
int size = substrings.length;
for ( int i = 0 ; i < size- 1 ; i++) {
for ( int j = i + 1 ; j < substrings.length; j++)
{
if (substrings[i].compareTo(substrings[j]) > 0 )
{
String temp = substrings[i];
substrings[i] = substrings[j];
substrings[j] = temp;
}
}
}
}
public static void main(String args[])
{
String s = "geek" ;
String substrings[] = new String[ 10 ];
pre_process(substrings, s);
int queries[] = { 1 , 5 , 10 };
int q = queries.length;
for ( int i = 0 ; i < q; i++)
System.out.println(substrings[queries[i]- 1 ]);
}
}
|
Python3
def pre_process(substrings, s) :
n = len (s);
for i in range (n) :
dup = "";
for j in range (i,n) :
dup + = s[j];
substrings.append(dup);
substrings.sort();
return substrings;
if __name__ = = "__main__" :
s = "geek" ;
substrings = [];
substrings = pre_process(substrings, s);
queries = [ 1 , 5 , 10 ];
q = len (queries);
for i in range (q) :
print (substrings[queries[i] - 1 ]);
|
C#
using System;
class GFG
{
static void pre_process(String []substrings,String s)
{
int n = s.Length;
int count = 0;
for ( int i = 0; i < n; i++)
{
String dup = "" ;
for ( int j = i; j < n; j++)
{
dup += s[j];
substrings[count++] = dup;
}
}
int size = substrings.Length;
for ( int i = 0; i < size-1; i++)
{
for ( int j = i + 1; j < substrings.Length; j++)
{
if (substrings[i].CompareTo(substrings[j]) > 0)
{
String temp = substrings[i];
substrings[i] = substrings[j];
substrings[j] = temp;
}
}
}
}
public static void Main(String []args)
{
String s = "geek" ;
String []substrings = new String[10];
pre_process(substrings, s);
int []queries = { 1, 5, 10 };
int q = queries.Length;
for ( int i = 0; i < q; i++)
Console.WriteLine(substrings[queries[i]-1]);
}
}
|
Javascript
<script>
function pre_process(substrings, s)
{
var n = s.length;
for ( var i = 0; i < n; i++) {
var dup = "" ;
for ( var j = i; j < n; j++) {
dup += s[j];
substrings.push(dup);
}
}
substrings.sort();
}
var s = "geek" ;
var substrings = [];
pre_process(substrings, s);
var queries = [1, 5, 10];
var q = queries.length;
for ( var i = 0; i < q; i++)
document.write( substrings[queries[i] - 1] + "<br>" );
</script>
|
Time Complexity: O(N2*logN), as we are using an inbuilt sort function to sort an array of size N*N. Where N is the length of the string.
Auxiliary Space: O(N2), as we are using extra space for storing the substrings.
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...