Python, don't look at words, look at chars in two virtual repeating strings, detailed explanation
Words repetition like [s1, n1] are distracting. What actually matters is the chars. \nImagine both s1 and s2 as "virtual" string that repeats forever. Let i1 an
Words repetition like [s1, n1] are distracting. What actually matters is the chars. \nImagine both s1 and s2 as "virtual" string that repeats forever. Let i1 and i2 be the index of the two virtual strings, not the index of the original short strings. That means i1 and i2 can be infinitely big. Use mod only when getting char value at i1 and i2, but don\'t change i1 and i2 by "mod" operation.\nMatch the two virtual s1 with s2, until the s1 index reach it\'s limit length, which by definition is n1\\*len(s1). Then the i2 position at this point will be the final result in terms of char length.\nThe rest is just converting i2 to the number of words, where word length is len(s2)\\*n2.\n\nTo improve performance, we use dictionary to store previously matched indexes in original s1 and s2, if the same matched index comes again, that means we are in a repeating pattern, in which case we increase i1 and i2 by the length of their repeating circle until there is not enough chars left in virtual s1.\n\n```\nclass Solution:\n    def getMaxRepetitions(self, s1: str, n1: int, s2: str, n2: int) -> int:\n        d={}\n        l1,l2=len(s1),len(s2)\n        total1=l1*n1\n        i1=i2=0\n        while i1<total1:\n            if s1[i1%l1]==s2[i2%l2]:\n                if (i1%l1,i2%l2) in d:\n                    prevI1,prevI2=d[(i1%l1,i2%l2)]\n                    circle1=i1-prevI1\n                    circle2=i2-prevI2\n                    circle1Cnt=(total1-i1)//circle1\n                    i1+=circle1Cnt*circle1\n                    i2+=circle1Cnt*circle2\n                    if i1>=total1:\n                        break\n                else:\n                    d[(i1%l1,i2%l2)]=[i1,i2]\n                i2+=1\n            i1+=1\n        return i2//l2//n2\n```\n
[]