Problem · String

Check a Repeated String as a Subsequence

Learn this problem
EasyQuince logoQuinceNEW GRADOA

Problem statement

Given two nonempty strings str1 and str2 and a positive integer k, consider the string formed by concatenating str2 with itself exactly k times.

You may delete any characters from str1 without changing the relative order of the remaining characters.

Return true if the repeated string is a subsequence of str1. Otherwise, return false.

Function

isRepeatedSubsequence(str1: String, str2: String, k: int) → boolean

Examples

Example 1

str1 = "abcabcabc"str2 = "abc"k = 2return = true

The first six characters of str1 form abcabc, which is str2 repeated twice.

Example 2

str1 = "abacb"str2 = "abc"k = 2return = false

The required repeated string has six characters, but str1 has only five characters, so it cannot be a subsequence.

Example 3

str1 = "axbyacbz"str2 = "ab"k = 2return = true

Keeping the characters at positions 0, 2, 4, and 6 produces abab.

Constraints

  • 1 <= str1.length, str2.length <= 100000
  • 1 <= k <= 100000
  • str1 and str2 contain only lowercase English letters.

More Quince problems

drafts saved locally
public boolean isRepeatedSubsequence(String str1, String str2, int k) {
  // write your code here
}
str1"abcabcabc"
str2"abc"
k2
expectedtrue
checking account