FastPrepString Subsequences 🐳

String Subsequences 🐳

Morgan Stanley logoMorgan Stanley● EasyINTERNOA
Learn

Problem statement

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.

Function

getSubsequenceCount(s1: String, s2: String) β†’ long

Complete the function getSubsequenceCount 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 s1 appears as a subsequence in s2

Examples

Example 1

s1 = "HRW"s2 = "HERHRWS"return = 3

HRW appears at 1-based positions (1, 3, 6), (1, 5, 6), and (4, 5, 6), so the count is 3.

Example 2

s1 = "ELO"s2 = "HELLOWORLD"return = 4

ELO appears at 1-based positions (2, 3, 5), (2, 3, 7), (2, 4, 5), and (2, 4, 7), so the count is 4.

Example 3

s1 = "ABC"s2 = "ABCBABC"return = 5

ABC appears at 1-based positions (1, 2, 3), (1, 2, 7), (1, 4, 7), (1, 6, 7), and (5, 6, 7), so the count is 5.

Constraints

  • length of s1 = 3
  • 1 ≀ length of s2 ≀ 5*105
  • s1 and s2 consist of uppercase English letters, A-Z.

More Morgan Stanley problems

See Morgan Stanley hiring insights
public long getSubsequenceCount(String s1, String s2) {
  // write your code here
}
s1"HRW"
s2"HERHRWS"
expected3
Checking account…