
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Queries to answer the X-th smallest sub-string lexicographically in C++
In this problem, we are given a string str and Q queries. Each Query has a number X. Our task is to create a program to solve the Queries to answer the X-th smallest sub-string lexicographically in C++.
Problem Description
We need to find the Xth lexicographically smallest substring for each query i.e. based on alphabetical order sorting we will have to find Xth substring.
Let’s take an example to understand the problem,
Input: str = “point”
Q = 4 query = {4, 7, 2, 13}
Output:n, oi, in, poin
Explanation
All substrings of str in lexicographical order are−
i, in, int, n, nt, o, oi, oin, oint, p, po, poi, poin, point, t
4th sub-string - n
7th sub-string - oi
2nd sub-string - in
13th sub-string - poin
Solution Approach
A simple solution will be generating all possible substrings of the string,storing them in a data structure, and then sorting them in lexicographical order i.e. alphabetical order. Then for the X in the queries, we need to print the corresponding subarray from the structure.
To store the substrings, we will use vectors.
Example
#include <bits/stdc++.h> using namespace std; vector<string> substrings; void find_SortSubstrings(string s) { int len = s.size(); for (int i = 0; i < len; i++) { string dup = ""; for (int j = i; j < len; j++) { dup += s[j]; substrings.push_back(dup); } } sort(substrings.begin(), substrings.end()); } int main(){ string str = "point"; find_SortSubstrings(str); int Q = 4; int query[] = { 4, 9, 5, 15 }; for (int i = 0; i < Q; i++) cout<<"Query "<<(i+1)<<" : The "<<query[i]<<"th smallest sub-string lexicographically is "<<substrings[query[i] - 1] << endl; return 0; }
Output
Query 1 : The 4th smallest sub-string lexicographically is n Query 2 : The 9th smallest sub-string lexicographically is oint Query 3 : The 5th smallest sub-string lexicographically is nt Query 4 : The 15th smallest sub-string lexicographically is t
- Related Articles
- Lexicographically Smallest Equivalent String in C++
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number in C++ Program
- Program to find kth smallest n length lexicographically smallest string in python
- Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++
- Program to find lexicographically smallest non-palindromic string in Python
- Program to find Lexicographically Smallest String With One Swap in Python
- Program to find lexicographically smallest string after applying operations in Python
- Find the lexicographically smallest string which satisfies the given condition in Python
- Program to find lexicographically smallest string to move from start to destination in Python
- Queries to find the last non-repeating character in the sub-string of a given string in C++
- Program to find lexicographically smallest lowercase string of length k and distance n in Python
- JavaScript Program to Find Lexicographically smallest rotated sequence
- Program to find lexicographically smallest subsequence of size k in Python
- Find n-th lexicographically permutation of a strings in Python
- Find n-th lexicographically permutation of a strings in C++
