Count The Repetitions - Problem
Count The Repetitions - A fascinating string matching problem that challenges your understanding of pattern recognition and optimization!

Imagine you have two super strings created by repeating base strings multiple times:
โ€ข str1 = [s1, n1] means string s1 repeated n1 times
โ€ข str2 = [s2, n2] means string s2 repeated n2 times

Your mission: Find the maximum number m such that str2 repeated m times can be obtained as a subsequence from str1.

Key Insight: A subsequence means you can remove some characters (but keep the relative order) to transform one string into another.

Example: If s1="acb", n1=4 gives us "acbacbacbacb"
If s2="ab", n2=2 gives us "abab"
We need to find how many times "abab" can fit as a subsequence in the first string.

Input & Output

example_1.py โ€” Basic Case
$ Input: s1 = "acb", n1 = 4, s2 = "ab", n2 = 2
โ€บ Output: 2
๐Ÿ’ก Note: str1 = "acbacbacbacb" (acb repeated 4 times). We need str2 = "abab" (ab repeated 2 times). We can extract "abab" twice from str1: first 'a' from pos 0, first 'b' from pos 2, second 'a' from pos 3, second 'b' from pos 5, giving us one "abab". Similarly for the second "abab". Total = 2.
example_2.py โ€” No Match Case
$ Input: s1 = "acb", n1 = 1, s2 = "acb", n2 = 1
โ€บ Output: 1
๐Ÿ’ก Note: str1 = "acb" and str2 = "acb". Since they are identical, str2 appears exactly once as a subsequence of str1.
example_3.py โ€” Impossible Case
$ Input: s1 = "abc", n1 = 4, s2 = "def", n2 = 1
โ€บ Output: 0
๐Ÿ’ก Note: str1 = "abcabcabcabc" contains no characters from str2 = "def", so it's impossible to form str2 as a subsequence.

Visualization

Tap to expand
Cycle Detection in String MatchingPhase 1: Trackโ€ข Match char by charโ€ข Record statesโ€ข Look for patternsPhase 2: Detectโ€ข Find repeated stateโ€ข Measure cycle lengthโ€ข Calculate gainPhase 3: Skipโ€ข Count full cyclesโ€ข Multiply progressโ€ข Add to resultExample: s1="acb", n1=4, s2="ab", n2=2str1 = "acbacbacbacb", target = "abab"s1[0] donematched: as1[1] donematched: abCycle detected!Skip aheadResult: We can fit 2 complete "abab" strings in "acbacbacbacb"โœ“ Optimization: Instead of checking all characters, we detected the cycle and calculated mathematically
Understanding the Visualization
1
Initial Matching
Start matching characters from s2 within repeated s1 strings, tracking progress
2
State Recording
Record how many s1 repetitions used and s2 characters matched for each s2 position
3
Cycle Detection
When we see the same s2 position again, we've found a repeating cycle
4
Mathematical Skip
Calculate how many cycles fit in remaining repetitions and skip ahead
Key Takeaway
๐ŸŽฏ Key Insight: By detecting cycles in the matching pattern, we transform a potentially exponential character-by-character simulation into an efficient mathematical calculation, making the solution scalable for large inputs.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(|s1| * |s2|)

In the worst case, we need to process at most |s1| * |s2| states before finding a cycle

n
2n
โœ“ Linear Growth
Space Complexity
O(|s2|)

We store state information for at most |s2| different positions

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค s1.length, s2.length โ‰ค 100
  • 1 โ‰ค n1, n2 โ‰ค 106
  • s1 and s2 consist of lowercase English letters
  • Time limit: The solution must handle large n1, n2 values efficiently
Asked in
Google 15 Amazon 8 Meta 6 Microsoft 4
31.2K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen