# Queries on subsequence of string

Given a string S and Q queries, each query contains a string T. The task is print “Yes” if T is 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

```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 be O(Q*N), N is the length of S.

The efficient approach can be if we know the position of next character of T in S. Then simply skip all the character between current and position of 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 above idea :

## C++

 `// C++ program to answer subsequence queries for a ` `// given string. ` `#include ` `#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 `

## 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. `

Output:

```Yes
No
Yes
No
```

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : vt_m

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.