K-th lexicographically smallest unique substring of a given string
Last Updated :
27 Apr, 2023
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++
// C++ implementation of the above approach
#include<bits/stdc++.h>
using namespace std;
void kThLexString(string st, int k, int n)
{
// Set to store the unique substring
set<string> z;
for(int i = 0; i < n; i++)
{
// String to create each substring
string pp;
for(int j = i; j < i + k; j++)
{
if (j >= n)
break;
pp += st[j];
// Adding to set
z.insert(pp);
}
}
// Converting set into a list
vector<string> fin(z.begin(), z.end());
// Sorting the strings int the list
// into lexicographical order
sort(fin.begin(), fin.end());
// Printing kth substring
cout << fin[k - 1];
}
// Driver code
int main()
{
string s = "geeksforgeeks";
int k = 5;
int n = s.length();
kThLexString(s, k, n);
}
// This code is contributed by yatinagg
Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
public static void kThLexString(String st,
int k, int n)
{
// Set to store the unique substring
Set<String> z = new HashSet<String>();
for(int i = 0; i < n; i++)
{
// String to create each substring
String pp = "";
for(int j = i; j < i + k; j++)
{
if (j >= n)
break;
pp += st.charAt(j);
// Adding to set
z.add(pp);
}
}
// Converting set into a list
Vector<String> fin = new Vector<String>();
fin.addAll(z);
// Sorting the strings int the list
// into lexicographical order
Collections.sort(fin);
// Printing kth substring
System.out.print(fin.get(k - 1));
}
// Driver Code
public static void main(String[] args)
{
String s = "geeksforgeeks";
int k = 5;
int n = s.length();
kThLexString(s, k, n);
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 implementation of the above approach
def kThLexString(st, k, n):
# Set to store the unique substring
z = set()
for i in range(n):
# String to create each substring
pp = ""
for j in range(i, i + k):
if (j >= n):
break
pp += s[j]
# adding to set
z.add(pp)
# converting set into a list
fin = list(z)
# sorting the strings int the list
# into lexicographical order
fin.sort()
# printing kth substring
print(fin[k - 1])
s = "geeksforgeeks"
k = 5
n = len(s)
kThLexString(s, k, n)
C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
using System.Collections;
class GFG{
public static void kThLexString(string st,
int k, int n)
{
// Set to store the unique substring
HashSet<string> z = new HashSet<string>();
for(int i = 0; i < n; i++)
{
// String to create each substring
string pp = "";
for(int j = i; j < i + k; j++)
{
if (j >= n)
break;
pp += st[j];
// Adding to set
z.Add(pp);
}
}
// Converting set into a list
ArrayList fin = new ArrayList();
foreach(string s in z)
{
fin.Add(s);
}
// Sorting the strings int the list
// into lexicographical order
fin.Sort();
// Printing kth substring
Console.Write(fin[k - 1]);
}
// Driver Code
public static void Main(string[] args)
{
string s = "geeksforgeeks";
int k = 5;
int n = s.Length;
kThLexString(s, k, n);
}
}
// This code is contributed by rutvik_56
JavaScript
<script>
// JavaScript implementation of the above approach
function kThLexString(st, k, n)
{
// Set to store the unique substring
var z = new Set();
for(var i = 0; i < n; i++)
{
// String to create each substring
var pp = "";
for(var j = i; j < i + k; j++)
{
if (j >= n)
break;
pp += st[j];
// Adding to set
z.add(pp);
}
}
var fin = [];
z.forEach(element => {
fin.push(element);
});
fin.sort();
// Printing kth substring
document.write( fin[k-1]);
}
// Driver code
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)
Similar Reads
Lexicographically smallest string which is not a subsequence of given string Given a string S, the task is to find the string which is lexicographically smallest and not a subsequence of the given string S. Examples: Input: S = "abcdefghijklmnopqrstuvwxyz"Output: aaExplanation:String "aa" is the lexicographically smallest string which is not present in the given string as a
5 min read
Lexicographically smallest K-length subsequence from a given string Given a string s of length n, the task is to find the lexicographically smallest k-length subsequence from the string s (where k < n). Examples:Input: s = "bbcaab", k = 3Output: "aab"Input: s = "aabdaabc", k = 3Output: "aaa"[Naive Approach] Generating all Subsequences - O(2^n) time and O(C(n,k)*k
9 min read
Kth lexicographically smallest distinct String from given Array of Strings Given an array arr having N strings and an integer K, the task is to find the lexicographically smallest Kth distinct string. Print an empty string if no such string exists. Example: Input: arr[]={"aa", "aa", "bb", "cc", "dd", "cc"}, K = 2Output: ddExplanation: Distinct strings are: "bb", "dd". 2nd
4 min read
Minimum size lexicographically smallest string which is not a substring of given string Given a string s, the task is to find the lexicographically smallest string of minimum characters that do not exist as a substring in S. Examples: Input: S = "aabacdefghijklmnopqrstuvwxyz"Output: adExplanation: All the single digit strings from [a-z] occur in the given string and in two character st
7 min read
Lexicographically smallest string formed by reversing Substrings of string S exactly K times Given a string S and an integer K, the task is to find the lexicographically smallest string possible after reversing any substring of any length exactly K times. Examples: Input: S = "fgazcbdfge", K = 3Output: abcdgfzfgeExplanation: After 1st operation: S = "agfzcbdfge", in S select S[0 - 2] = "fga
11 min read
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 p
5 min read
Lexicographically Kth-smallest string having 'a' X times and 'b' Y times Given three non-negative integers, X, Y, and K, the task is to find the Kth smallest lexicographical string having X occurrences of character 'a' and Y occurrences of character 'b'. Examples: Input: X = 2, Y = 3, K = 3Output: abbabExplanation: First lexicographical smallest string = "aabbb".Second l
15+ min read
Lexicographically smallest string with period K possible by replacing '?'s from a given string Given a string S consisting of N lowercase characters and character '?' and a positive integer K, the task is to replace each character '?' with some lowercase alphabets such that the given string becomes a period of K. If it is not possible to do so, then print "-1". A string is said to be a period
9 min read
Frequency of lexicographically Kth smallest character in the a string Given a string S of length N and an integer K, the task is to find the frequency of the lexicographically Kth smallest character present in the given string. Examples: Input: S = "geeksforgeeks", K = 3Output: 4Explanation: The lexicographically 3rd smallest character in S is 'e'. Frequency of 'e' in
5 min read
Lexicographically shortest string of length at most K which is not a substring of given String Given a string S, the task is to find the lexicographically shortest string of length less than or equal to K which is not a substring of the given string. If not possible, print -1. Examples: Input: S = zxabcehgf, K = 2Output: dExplanation: Lexicographically, the shortest string which is not a subs
9 min read