Queries on subsequence of string
Last Updated :
12 Sep, 2023
Given a string S and Q queries, each query contains a string T. The task is to print "Yes" if T is a subsequence of S, else print "No".
Examples:
Input : S = "geeksforgeeks"
Query 1: "gg"
Query 2: "gro"
Query 3: "gfg"
Query 4: "orf"
Output :
Yes
No
Yes
No
For each query, using the brute force, start iterating over S looking for the first character of T. As soon as, the first character is found, continue to iterate S now looking for the second character of T and so on (Refer this for details). If manage to find all the character of T, print "Yes", else "No". Time complexity is O(Q*N), N is the length of S.
The efficient approach can be if we know the position of the next character of T in S. Then simply skip all the character between current and position of the next character and jump to that position. This can be done by making |S| x 26 size matrix and storing the next position of each character from every position of S.
Below is the implementation of the above idea :
C++
// C++ program to answer subsequence queries for a
// given string.
#include <bits/stdc++.h>
#define MAX 10000
#define CHAR_SIZE 26
using namespace std;
// Precompute the position of each character from
// each position of String S
void precompute(int mat[MAX][CHAR_SIZE], char str[],
int len)
{
for (int i = 0; i < CHAR_SIZE; ++i)
mat[len][i] = len;
// Computing position of each character from
// each position of String S
for (int i = len-1; i >= 0; --i)
{
for (int j = 0; j < CHAR_SIZE; ++j)
mat[i][j] = mat[i+1][j];
mat[i][str[i]-'a'] = i;
}
}
// Print "Yes" if T is subsequence of S, else "No"
bool query(int mat[MAX][CHAR_SIZE], const char *str,
int len)
{
int pos = 0;
// Traversing the string T
for (int i = 0; i < strlen(str); ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos][str[i] - 'a'] >= len)
return false;
// Setting position of next character
else
pos = mat[pos][str[i] - 'a'] + 1;
}
return true;
}
// Driven Program
int main()
{
char S[]= "geeksforgeeks";
int len = strlen(S);
int mat[MAX][CHAR_SIZE];
precompute(mat, S, len);
query(mat, "gg", len)? cout << "Yes\n" :
cout << "No\n";
query(mat, "gro", len)? cout << "Yes\n" :
cout << "No\n";
query(mat, "gfg", len)? cout << "Yes\n" :
cout << "No\n";
query(mat, "orf", len)? cout << "Yes\n" :
cout << "No\n";
return 0;
}
Java
// Java program to answer subsequence queries for
// a given string.
public class Query_Subsequence {
static final int MAX = 10000;
static final int CHAR_SIZE = 26;
// Precompute the position of each character from
// each position of String S
static void precompute(int mat[][], String str, int len)
{
for (int i = 0; i < CHAR_SIZE; ++i)
mat[len][i] = len;
// Computing position of each character from
// each position of String S
for (int i = len-1; i >= 0; --i)
{
for (int j = 0; j < CHAR_SIZE; ++j)
mat[i][j] = mat[i+1][j];
mat[i][str.charAt(i)-'a'] = i;
}
}
// Print "Yes" if T is subsequence of S, else "No"
static boolean query(int mat[][], String str, int len)
{
int pos = 0;
// Traversing the string T
for (int i = 0; i < str.length(); ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos][str.charAt(i) - 'a'] >= len)
return false;
// Setting position of next character
else
pos = mat[pos][str.charAt(i) - 'a'] + 1;
}
return true;
}
// Driven Program
public static void main(String args[])
{
String S= "geeksforgeeks";
int len = S.length();
int[][] mat = new int[MAX][CHAR_SIZE];
precompute(mat, S, len);
String get = query(mat, "gg", len)? "Yes" :"No";
System.out.println(get);
get = query(mat, "gro", len)? "Yes" :"No";
System.out.println(get);
get = query(mat, "gfg", len)? "Yes" :"No";
System.out.println(get);
get = query(mat, "orf", len)? "Yes" :"No";
System.out.println(get);
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to answer
# subsequence queries for
# a given string.
MAX = 10000
CHAR_SIZE = 26
# Precompute the position of
# each character from
# each position of String S
def precompute(mat, str, Len):
for i in range(CHAR_SIZE):
mat[Len][i] = Len
# Computing position of each
# character from each position
# of String S
for i in range(Len - 1, -1, -1):
for j in range(CHAR_SIZE):
mat[i][j] = mat[i + 1][j]
mat[i][ord(str[i]) -
ord('a')] = i
# Print "Yes" if T is
# subsequence of S, else "No"
def query(mat, str, Len):
pos = 0
# Traversing the string T
for i in range(len(str)):
# If next position is greater than
# length of S set flag to false.
if(mat[pos][ord(str[i]) -
ord('a')] >= Len):
return False
# Setting position of next character
else:
pos = mat[pos][ord(str[i]) -
ord('a')] + 1
return True
# Driven code
S = "geeksforgeeks"
Len = len(S)
mat = [[0 for i in range(CHAR_SIZE)]
for j in range(MAX)]
precompute(mat, S, Len)
get = "No"
if(query(mat, "gg", Len)):
get = "Yes"
print(get)
get = "No"
if(query(mat, "gro", Len)):
get = "Yes"
print(get)
get = "No"
if(query(mat, "gfg", Len)):
get = "Yes"
print(get)
get = "No"
if(query(mat, "orf", Len)):
get = "Yes"
print(get)
# This code is contributed by avanitrachhadiya2155
C#
// C# program to answer subsequence
// queries for a given string
using System;
public class Query_Subsequence
{
static int MAX = 10000;
static int CHAR_SIZE = 26;
// Precompute the position of each
// character from each position
// of String S
static void precompute(int [,]mat,
string str,
int len)
{
for (int i = 0; i < CHAR_SIZE; ++i)
mat[len, i] = len;
// Computing position of each
// character from each position
// of String S
for (int i = len - 1; i >= 0; --i)
{
for (int j = 0; j < CHAR_SIZE;
++j)
mat[i, j] = mat[i + 1, j];
mat[i, str[i] - 'a'] = i;
}
}
// Print "Yes" if T is subsequence
// of S, else "No"
static bool query(int [,]mat,
string str,
int len)
{
int pos = 0;
// Traversing the string T
for (int i = 0; i < str.Length; ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos,str[i] - 'a'] >= len)
return false;
// Setting position of next character
else
pos = mat[pos,str[i] - 'a'] + 1;
}
return true;
}
// Driver Code
public static void Main()
{
string S= "geeksforgeeks";
int len = S.Length;
int[,] mat = new int[MAX,CHAR_SIZE];
precompute(mat, S, len);
string get = query(mat, "gg", len)?
"Yes" :"No";
Console.WriteLine(get);
get = query(mat, "gro", len)?
"Yes" :"No";
Console.WriteLine(get);
get = query(mat, "gfg", len)?
"Yes" :"No";
Console.WriteLine(get);
get = query(mat, "orf", len)?
"Yes" :"No";
Console.WriteLine(get);
}
}
// This code is contributed by vt_m.
JavaScript
<script>
// Javascript program to answer subsequence queries for
// a given string.
let MAX = 10000;
let CHAR_SIZE = 26;
// Precompute the position of each character from
// each position of String S
function precompute(mat, str, len)
{
for (let i = 0; i < CHAR_SIZE; ++i)
mat[len][i] = len;
// Computing position of each character from
// each position of String S
for (let i = len-1; i >= 0; --i)
{
for (let j = 0; j < CHAR_SIZE; ++j)
mat[i][j] = mat[i+1][j];
mat[i][str[i].charCodeAt()-'a'.charCodeAt()] = i;
}
}
// Print "Yes" if T is subsequence of S, else "No"
function query(mat, str, len)
{
let pos = 0;
// Traversing the string T
for (let i = 0; i < str.length; ++i)
{
// If next position is greater than
// length of S set flag to false.
if (mat[pos][str[i].charCodeAt() - 'a'.charCodeAt()] >= len)
return false;
// Setting position of next character
else
pos = mat[pos][str[i].charCodeAt() - 'a'.charCodeAt()] + 1;
}
return true;
}
let S= "geeksforgeeks";
let len = S.length;
let mat = new Array(MAX);
for(let i = 0; i < MAX; i++)
{
mat[i] = new Array(CHAR_SIZE);
for(let j = 0; j < CHAR_SIZE; j++)
{
mat[i][j] = 0;
}
}
precompute(mat, S, len);
let get = query(mat, "gg", len)? "Yes" :"No";
document.write(get + "</br>");
get = query(mat, "gro", len)? "Yes" :"No";
document.write(get + "</br>");
get = query(mat, "gfg", len)? "Yes" :"No";
document.write(get + "</br>");
get = query(mat, "orf", len)? "Yes" :"No";
document.write(get + "</br>");
</script>
Similar Reads
Print all subsequences of a string Given a string, we have to find out all its subsequences of it. A String is said to be a subsequence of another String, if it can be obtained by deleting 0 or more character without changing its order.Examples: Input : abOutput : "", "a", "b", "ab"Input : abcOutput : "", "a", "b", "c", "ab", "ac", "
12 min read
Check if one string is subsequence of other Given two strings s1 and s2, find if the first string is a Subsequence of the second string, i.e. if s1 is a subsequence of s2. A subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.Examples : Input: s1 =
10 min read
Count common subsequence in two strings Given two string S and T. The task is to count the number of the common subsequence in S and T. Examples: Input : S = "ajblqcpdz", T = "aefcnbtdi" Output : 11 Common subsequences are : { "a", "b", "c", "d", "ab", "bd", "ad", "ac", "cd", "abd", "acd" } Input : S = "a", T = "ab" Output : 1 To find the
11 min read
Longest Repeating Subsequence Given a string s, the task is to find the length of the longest repeating subsequence, such that the two subsequences don't have the same string character at the same position, i.e. any ith character in the two subsequences shouldn't have the same index in the original string. Examples:Input: s= "ab
15+ min read
Count Distinct Subsequences Given a string str of length n, your task is to find the count of distinct subsequences of it.Examples: Input: str = "gfg"Output: 7Explanation: The seven distinct subsequences are "", "g", "f", "gf", "fg", "gg" and "gfg" Input: str = "ggg"Output: 4Explanation: The four distinct subsequences are "",
13 min read
Subsequence queries after removing substrings Given two strings A and B, the problem is to find if string B will be a subsequence of string A if we remove the substring [A[i]..A[j]] from string A. Assume that there are Q queries giving the indices i and j and each query is independent of the other. Examples: Input : A = abcabcxy, B = acy Q = 2
9 min read
Subsequence meaning in DSA A subsequence is defined as a sequence that can be derived from another string/sequence by deleting some or none of the elements without changing the order of the remaining elements. For example: Let's take "GeeksForGeeks", GeeksF will be a subsequence of "GeeksForGeeks". Example of SubsequencePrope
2 min read
Longest Common Subsequence (LCS) Given two strings, s1 and s2, the task is to find the length of the Longest Common Subsequence. If there is no common subsequence, return 0. A subsequence is a string generated from the original string by deleting 0 or more characters, without changing the relative order of the remaining characters.
15+ min read
Print all subsequences of a string in Python Given a string s of size n (1 ⤠n ⤠20), the task is to print all subsequences of string. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.Examples: Input: s = "abc"Output: ["", "a", "b", "c", "
3 min read
Count subsequence of length three in a given string Given a string of length n and a subsequence of length 3. Find the total number of occurrences of the subsequence in this string. Examples : Input : string = "GFGFGYSYIOIWIN", subsequence = "GFG" Output : 4 Explanation : There are 4 such subsequences as shown: GFGFGYSYIOIWIN GFGFGYSYIOIWIN GFGFGYSYI
15 min read