16. String Subsequences
Given two strings, determine the number of times
the first one appears as a subsequence in the
second one. A subsequence is created by
eliminating any number of characters from a string
(possibly 0) without changing the order of the
characters retained.
For example, let's say s1 = "ABC" and s2=
"ABCBABC". The first string appears 5 times as a
Subsequence in the second string at 1-indexed
positions of (1, 2, 3), (1, 2, 7), (1,4, 7), (1. 6, 7), and (5,
6, 7). Therefore, the answer is 5.
Function Description
Complete the function getSubsequence Count in the
editor below.
getsubsequenceCount has the following
parameters:
string s1: the first string, which always has a
length of 3
string s2: the second string
Returns:
int: the number of times sl appears as a
subsequence in s22
Constraints
length.of si- a
Sample Input For Custom Testing
Function
STDIN
HRW
s1 "HRW"
HERHRWS s2 "HERHRWS"
Sample Output
3
Explanation
"HRW appears as a subsequence in "HERHRWS"3
times at positions (1,3, 6), (1, 5, 6), and (4, 5, 6).
Therefore, the answer is 3.
Returns:
int: the number of times s1 appears as a
subsequence in s2
Constraints
length of s13
1s length of s2s 5*10
s1 and s2 consist of uppercase English
letters, asci[A-Z]
Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
STDIN
Function
HRW
S1 "HRW
HERHRWS s2 "HERHRWS"
Sample Output
Explanation
HRW appears as a subsequence in "HERHRWS" 3
times at positions (1, 3, 6), (1, 5, 6), and (4, 5, 6).
Therefore, the answer is 3.
Sample Case 1
Sample Input For Custom Testing
STDIN
Function
ELO
sl "ELO"
HELLOWORLD
s2"HELLOWORLD"
Sample Output
Explanation
ELO appears as a subsequence in
"HELLOWORLD" 4 times at positions (2, 3, 5), (2, 3,
7,2,4, 5), and (2, 4, 7). Therefore I
Answers
Answer:
Given two strings, determine the number of times
the first one appears as a subsequence in the
second one. A subsequence is created by
eliminating any number of characters from a string
(possibly 0) without changing the order of the
characters retained.
For example, let's say s1 = "ABC" and s2=
"ABCBABC". The first string appears 5 times as a
Subsequence in the second string at 1-indexed
positions of (1, 2, 3), (1, 2, 7), (1,4, 7), (1. 6, 7), and (5,
6, 7). Therefore, the answer is 5.
Function Description
Complete the function getSubsequence Count in the
editor below.
getsubsequenceCount has the following
parameters:
string s1: the first string, which always has a
length of 3
string s2: the second string
Returns:
int: the number of times sl appears as a
subsequence in s22
Constraints
length.of si- a
Sample Input For Custom Testing
Function
STDIN
HRW
s1 "HRW"
HERHRWS s2 "HERHRWS"
Sample Output
3
Explanation
"HRW appears as a subsequence in "HERHRWS"3
times at positions (1,3, 6), (1, 5, 6), and (4, 5, 6).
Therefore, the answer is 3.
Returns:
int: the number of times s1 appears as a
subsequence in s2
Constraints
length of s13
1s length of s2s 5*10
s1 and s2 consist of uppercase English
letters, asci[A-Z]
Input Format For Custom Testing
Sample Case 0
Sample Input For Custom Testing
STDIN
Function
HRW
S1 "HRW
HERHRWS s2 "HERHRWS"
Sample Output
Explanation
HRW appears as a subsequence in "HERHRWS" 3
times at positions (1, 3, 6), (1, 5, 6), and (4, 5, 6).
Therefore, the answer is 3.
Sample Case 1
Sample Input For Custom Testing
STDIN
Function
ELO
sl "ELO"
HELLOWORLD
s2"HELLOWORLD"
Sample Output
Explanation
ELO appears as a subsequence in
"HELLOWORLD" 4 times at positions (2, 3, 5), (2, 3,
7,2,4, 5), and (2, 4, 7). Therefore I