Python 3 Solution with Clear Comments
\nclass Solution:\n    def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:\n        # hashtable to store the patterns\n        patterns = {}
```\nclass Solution:\n    def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:\n        # hashtable to store the patterns\n        patterns = {}\n\n        # pointers on s1 and s2\n        p1, p2 = 0, 0\n\n        # number of occurance of s1 and s2 so far\n        c1, c2 = 1, 0\n\n        # execute the loop when number of occurance of s1 has not been used up\n        while c1 <= n1:\n\n            # if a character match is found, move p2 forward\n            if s1[p1] == s2[p2]:\n                p2 += 1\n\n                # p2 reaches the end of s2, meaning 1 occurance of s2\n            if p2 == len(s2):\n                c2 += 1\n                p2 = 0\n\n                # store the pattern if not exists\n                if p1 not in patterns:\n                    patterns[p1] = (c1, c2)\n\n                # a repeat has been found, handle the repeat\n                else:\n                    # previous occurance of s1 and s2\n                    prev_c1, prev_c2 = patterns[p1]\n\n                    # number of occurance of s1 and s2 in a single repeat\n                    n_s1_repeat, n_s2_repeat = c1 - prev_c1, c2 - prev_c2\n\n                    # number of repeats\n                    n_repeats = (n1 - prev_c1) // n_s1_repeat\n\n                    # number of s2 occurances in the repeats\n                    c2_repeats = n_repeats * n_s2_repeat\n\n                    # the remain available occurances of s1\n                    remain_c1 = (n1 - prev_c1) % n_s1_repeat\n\n                    # update c1 and c2\n                    c1 = n1 - remain_c1\n                    c2 = c2_repeats + prev_c2\n\n            # move forward p1 every iteration\n            p1 += 1\n\n            # p1 reaches the end of s1, move it back to 0 and mark 1 occurance of s1\n            if p1 == len(s1):\n                c1 += 1\n                p1 = 0\n\n        # divide c2 by n2 to get the result\n        return c2 // n2\n```
[]