Leetcode Problem 466. Count The Repetitions

466. Count The Repetitions

Leetcode Solutions

Pattern Recognition and Repetition Counting

  1. Initialize count and index to 0, which represent the number of times s2 is found in str1 and the current index in s2, respectively.
  2. Initialize two arrays indexr and countr of size size(s2) + 1, with all elements set to 0. These arrays will store the index and count at the start of each s1 block.
  3. Iterate over i from 0 to n1 - 1: a. For each character in s1, increment index if the character matches the current character in s2. b. If index equals size(s2), reset index to 0 and increment count. c. Store the current count and index in countr[i] and indexr[i].
  4. Check for repetition by iterating over k from 0 to i - 1: a. If indexr[k] equals the current index, calculate the counts for the block before repetition (prev_count), the repeating block (pattern_count), and the block after repetition (remain_count). b. Sum the three counts and return the sum divided by n2.
  5. If no repetition is found, return countr[n1 - 1] / n2.
UML Thumbnail

Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...