Cover image for Interview Classic 150 Questions P392 Is Subsequence

Interview Classic 150 Questions P392 Is Subsequence


Timeline

timeline

2025-10-15

init

two pointers, dynamic programming

Problem:

Two Pointers

Two pointers point to s and t respectively. If they match, both pointers move forward; if not, the pointer pointing to t moves forward. Note that empty strings need to be handled separately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
using std::string;

class Solution {
public:
bool isSubsequence(string s, string t)
{
// Determine if s is a subsequence of t
int i = 0, j = 0;
int s_len = s.size();
int t_len = t.size();

if (s_len == 0) {
return true;
}
while (i < s_len && j < t_len) {
if (s[i] == t[j]) {
i++;
}
j++;
}
if (i == s_len && s[i - 1] == t[j - 1]) {
return true;
}
return false;
}
};

#include <stdio.h>
int main()
{
Solution solution;
string s = "abc";
string t = "ahbgdc";
printf("%d\n", solution.isSubsequence(s, t));
}

Dynamic Programming

Considering the previous two-pointer approach, we notice that we spend a lot of time finding the next matching character in t. Thus, we can preprocess for each position in t the first occurrence of each character from that position onward. We can use dynamic programming to implement the preprocessing:

Let f[i][j]f[i][j] denote the first occurrence of character j starting from position i in string t.

During state transition: if the character at position i in t is j, thenf[i][j]=if[i][j]=i, otherwise j appears starting from position i+1 onwards, i.e.,f[i][j]=f[i+1][j]f[i][j]=f[i+1][j], therefore we need to construct dp from back to front

The state transition equation is:

f[i][j]={it[i]=jf[i+1][j]t[i]jf[i][j] = \begin{cases} i & t[i] = j \\ f[i+1][j] & t[i] \neq j \end{cases}

The boundary conditions are:

f[m][j]=mj{0,1,,25}f[m][j] = m \quad \forall j \in \{0, 1, \dots, 25\}

where:

  • iidenotes the stringttposition in (starting from 0, 0 ≤ i ≤ m)
  • jjdenotes the character index (usually ‘a’ corresponds to 0, ‘b’ to 1, …, ‘z’ to 25)
  • f[i][j]=mf[i][j] = mdenotes from positioniionwards there is no characterjj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <vector>
#include <string>

using std::vector;
using std::string;

class Solution {
public:
bool isSubsequence(string s, string t)
{
int n = s.size(), m = t.size();
// m+1 is to handle boundary cases
vector<vector<int> > f(m + 1, vector<int>(26, 0));
for (int i = 0; i < 26; i++) {
f[m][i] = m;
}

for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j < 26; j++) {
if (t[i] == j + 'a')
f[i][j] = i;
else
f[i][j] = f[i + 1][j];
}
}
int add = 0;
for (int i = 0; i < n; i++) {
if (f[add][s[i] - 'a'] == m) {
return false;
}
add = f[add][s[i] - 'a'] + 1;
}
return true;
}
};