Skip to content
Problem 466 · LeetCode 466(opens in a new tab)

Count The Repetitions

HardStrings

Write [s, n] for the string s repeated n times, so ["xy", 3] is "xyxyxy". A string a can be taken from a string b if you can delete some letters of b (maybe none) and be left with a, keeping the order.

You get two strings, s1 and s2, and two counts, n1 and n2. Build str1 by repeating s1 n1 times, and str2 by repeating s2 n2 times. Find the largest m for which str2 written m times in a row can be taken from str1.

Example 1

Input:  s1 = "abc", n1 = 7, s2 = "cab", n2 = 2
Output: 3
Why:    "abc" × 7 holds 6 copies of "cab"; str2 is 2 of them
2 more examples and the constraints

Example 2

Input:  s1 = "xyz", n1 = 3, s2 = "xq", n2 = 1
Output: 0
Why:    s1 has no "q", so not even one copy fits

Example 3

Input:  s1 = "bca", n1 = 2, s2 = "bca", n2 = 1
Output: 2
Why:    str1 is exactly two copies of str2

Constraints

  • 1 ≤ length of s1, length of s2 ≤ 100
  • s1 and s2 hold only lowercase English letters
  • 1 ≤ n1, n2 ≤ 1,000,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Simulate letter by letterO(n1 · |s1|)
  2. Find the repeating blockO(|s1| · |s2|)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeLetter checks for n1 = 1,000,000 and |s1| = |s2| = 100Verdict
Simulate letter by letterO(n1 · |s1|)
Slow at the limits
Find the repeating blockO(|s1| · |s2|)
Best; at most |s2| + 1 blocks

|s1| and |s2| are the string lengths.

Edge cases to test

  • A letter of s2 missing from s1. No copy ever finishes. The answer is 0, and the cycle code still ends because j repeats right away.
  • n2 larger than the copies found. Integer division gives 0.
  • A lead-in before the cycle. The first blocks may not be part of the cycle. Measure the cycle from the block where j was first seen, not from block 0.
  • Big counts. count can reach about 100 million; it still fits in a 32-bit int.

Common mistakes

  • Dividing by n2 too early, per block, and losing partial copies that finish later.
  • Assuming the cycle starts at block 0, which miscounts when the first blocks are a lead-in.
  • Matching s2 with a search that restarts at the beginning of s2 on a mismatch; the greedy pointer only moves forward.

How to talk through it

  1. 1Clarify. Taken from means a subsequence, right? Can a copy of s2 run across the end of one s1 block into the next?
  2. 2Start with the simulation. Greedy matching with a pointer into s2 counts copies of s2; divide by n2 at the end.
  3. 3Name the bottleneck. n1 · |s1| can be 100 million letter checks.
  4. 4Spot the cycle. A block’s effect depends only on j at its start, and j has |s2| values. Store the first block for each j and jump when it repeats.
  5. 5Test out loud. Try a missing letter, n2 larger than the count, and a case where the cycle starts after block 0.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?