Given a String and an Integer k, find the kth Substring when all the Substrings are Sorted According to the given Condition


Introduction

In this tutorial, we implement an approach to find the kth substring after sorting all the substrings according to some conditions for a given string and the value of k. The condition to sort the substring is that the substrings are alphabetical while producing the substring in the order of their occurrence of each character in the alphabet. The first alphabet generates all its substrings, then the second alphabet produces all its substrings, and so on. Consider an example: the input string is “abc”, the alphabetically sorted substrings are “a”, “ab”, “abc”, “b”, “bc”, “c”. Predefined the value of k to generate that kth substring.

Demonstration 1

String = “bcd”
K = 2

Output

The kth value substring is “bc”

In the above demonstration, the input string is “bcd” and k is 2. The character which comes first among "bcd" is "b", it produces all its substrings, then "c" produces all its substrings and the last character is "d". The possible combinations of sorted substrings are “b”, “bc”, “bcd”, “c”, “cd”, “d”. The kth value of the substring is “bc”.

Demonstration 2

String = “bcd‘
K = 10

Output

No such substring is possible

In the above demonstration, the input string is “bcd” with a value of k equal to 10. The task is to generate the 10th substring using the string. The possible combinations of sorted substrings are “b”, “bc”, “bcd”, “c”, “cd”, “d”. There is no 10th substring. The output is No such substring is possible.

C++ Library Functions

Syntax

length() : this is a string class library function which returns the length of the string. The string length is the number of characters in the string.

 string_name.length();

vector() : It is a dynamic array in C++ and defined in a <vector> header file. It provides contiguous memory locations for its member elements.

 vector<data_type> vector_name; 

size() : It is a standard library function defined in the <std> header file. It returns the size of the string.

 string_name.size();

push_back() : It is a member function of the vector class. It is used to insert another element at the end of the inserted vector elements.

  vector_name.push_back(value);

begin() : It is a member function of vector class and returns the pointer of the starting element.

 vector_name.begin();

end() : It is a member function of vector class and returns the pointer of the last element.

vector_name.end();

substr() : It is a string class library function. It generates substrings using the input string. It takes two parameters: the starting value of the substring and the length of the substring.

 string_name.substr(pos, length);

Algorithm

  • Take an input string, and define k's value.

  • Generate all substrings by defining starting and ending values.

  • Now, find the kth substring by iterating all the generated substrings.

  • If the value of k is more than the total number of substrings, break the loop and print the output.

  • Print the substring of the value k.

Example 1

To implement the problem of finding the kth substring, we will use binary search. Binary search is a searching algorithm to find an element in stored elements.

To store the generated substrings create a string array that iterates from the xth character to the substring [x +1]. Applied a binary search algorithm to find the kth substring from the generated substring. Its C++ implementation is:

#include <bits/stdc++.h>
using namespace std;
 
// function to find the kth substring
void findKSubstring(string s, int l, int k)
{
 
    // Generating all susbtrings
    int totalSubstring = (l * (l + 1)) / 2;
 
    // when value of k is greater than total number of substrings
    if (k > totalSubstring) 
    {
        printf("No substring is possible at this value of k.");
        return;
    }
 
    // array to store substrings
    int arr_substring[l + 1];
    arr_substring[0] = 0;

    int t = l;
    for (int x = 1; x <= l; x++) 
     {
        arr_substring[x] = arr_substring[x - 1] + t;
        t--;
    }
 
    // using binary search to find the kth substring
    int m = 1;
    int i = l;
    int startIndex = 0;
 
    while (m <= i)
    {
        int n = (m + i) / 2;
 
        if (arr_substring[n] > k) 
        {
            startIndex = n;
            i = n - 1;
        }
 
        else if (arr_substring[n] < k)
            m = n + 1;
 
        else 
        {
            startIndex = n;
            break;
        }
    }
 
    int endIndex = l - (arr_substring[startIndex] - k);
 
    // Printing the kth substring
    for (int x = startIndex - 1; x < endIndex; x++)
        cout <<s[x];
}
 
// Code controller
int main()
{
    string s = "abcd";
    int k = 3;
    int l = s.length();
 
    findKSubstring(s, l, k);
 
    return 0;
}

Output

abc

Example 2

Implement the task of finding the kth substring from the generated alphabetically sorted substrings. In this approach, first, generate all substrings and sort them alphabetically. Iterate over all the substrings to find the index value of kth substrings. Print the results.

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string findSubstring(const string& s, int k)
{
    int l = s.length();
    vector<string> substrg;

    // Generate all possible substrings
    for (int x = 0; x < l; x++)
    {
        for (int y = 1; y <= l - x; y++) 
        {
            substrg.push_back(s.substr(x, y));
        }
    }

    // Sort the substrings in alphabetical order
    sort(substrg.begin(), substrg.end());

    // Check if k is within the range of substrings
    if (k >= 1 && k <= substrg.size()) 
    {
        return substrg[k - 1];
    } 
    else
    {
        return "Value of k is -1";
    }
}

int main() 
{
    string s = "abc"; // Predefined string
    int k = 5; // Predefined value for k

    // Find the kth substring
    string kthSubstr = findSubstring(s, k);

    // Display the result
    cout << "The kth substring is: " << kthSubstr << endl;

    return 0;
}

Output

The kth substring is bc.

Conclusion

We have reached the end of this tutorial for finding the kth substring among sorted substrings. The substrings are sorted alphabetically so that the letter which comes first in the alphabet forms its substrings. The next alphabetical letter is used to make substrings and so on. To generate all substrings an input string is used with a value of k.

We implemented this task with two examples, each with its own logic. Used C++ library functions to implement the problem statement.

Updated on: 18-Aug-2023

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements