Maximize count of occurrences of S2 in S1 as a subsequence by concatenating N1 and N2 times respectively | Function to count maximum number of occurrences of s2 as subsequence in s1 by concatenating s1 , n1 times and s2 , n2 times ; Stores number of times s1 is traversed ; Stores number of times s2 is traversed ; Mapping index of s2 to number of times s1 and s2 are traversed ; Stores index of s1 circularly ; Stores index of s2 circularly ; Traverse the string s1 , n1 times ; If current character of both the string are equal ; Update j ; Update i ; If j is length of s2 ; Update j for circular traversal ; Update s2_reps ; If i is length of s1 ; Update i for circular traversal ; Update s1_reps ; If already mapped j to ( s1_reps , s2_reps ) ; Mapping j to ( s1_reps , s2_reps ) ; If s1 already traversed n1 times ; Otherwis , traverse string s1 by multiple of s1_reps and update both s1_reps and s2_reps ; Update s2_reps ; Update s1_reps ; If s1 is traversed less than n1 times ; If current character in both the string are equal ; Update j ; Update i ; If i is length of s1 ; Update i for circular traversal ; Update s1_reps ; If j is length of ss ; Update j for circular traversal ; Update s2_reps ; Function to count maximum number of occurrences of s2 as subsequence in s1 by concatenating s1 , n1 times and s2 , n2 times
def getMaxRepetitions ( s1 , n1 , s2 , n2 ) : NEW_LINE INDENT if any ( c for c in set ( s2 ) if c not in set ( s1 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT s1_reps = 0 NEW_LINE DEDENT + Y4E4 : Z4C4E4 : W4E4 : Y4E4 : Z4E4 : AA4E4 : Z4E4 : Y4E4 : X4E4 : W4 NEW_LINE INDENT s2_reps = 0 NEW_LINE s2_index_to_reps = { 0 : ( 0 , 0 ) } NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while s1_reps < n1 : NEW_LINE INDENT if s1 [ i ] == s2 [ j ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE if j == len ( s2 ) : NEW_LINE INDENT j = 0 NEW_LINE s2_reps += 1 NEW_LINE DEDENT if i == len ( s1 ) : NEW_LINE INDENT i = 0 NEW_LINE s1_reps += 1 NEW_LINE if j in s2_index_to_reps : NEW_LINE INDENT break NEW_LINE DEDENT s2_index_to_reps [ j ] = ( s1_reps , s2_reps ) NEW_LINE DEDENT DEDENT if s1_reps == n1 : NEW_LINE INDENT return s2_reps // n2 NEW_LINE DEDENT initial_s1_reps , initial_s2_reps = s2_index_to_reps [ j ] NEW_LINE loop_s1_reps = s1_reps - initial_s1_reps NEW_LINE loop_s2_reps = s2_reps - initial_s2_reps NEW_LINE loops = ( n1 - initial_s1_reps ) NEW_LINE s2_reps = initial_s2_reps + loops * loop_s2_reps NEW_LINE s1_reps = initial_s1_reps + loops * loop_s1_reps NEW_LINE while s1_reps < n1 : NEW_LINE INDENT if s1 [ i ] == s2 [ j ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT i += 1 NEW_LINE if i == len ( s1 ) : NEW_LINE INDENT i = 0 NEW_LINE s1_reps += 1 NEW_LINE DEDENT if j == len ( s2 ) : NEW_LINE INDENT j = 0 NEW_LINE s2_reps += 1 NEW_LINE DEDENT DEDENT return s2_reps // n2 NEW_LINE DEDENT def getMaxRepetitions ( s1 , n1 , s2 , n2 ) : A4 : AJ4 NEW_LINE INDENT if any ( c for c in set ( s2 ) if c not in set ( s1 ) ) : NEW_LINE INDENT return 0 + E4 : W4 NEW_LINE DEDENT DEDENT