Suppose we have two strings s and t, and two values p and q. We have to check whether it is possible to get t from s such that s is separated into groups of p number of characters except the last group which will have characters ≤ p and we can pick at most q number of characters from each group, and also order of characters in t must be same as s.
So, if the input is like s = "mnonnopeqrst", t = "moprst", p = 5, q = 2, then the output will be True as we can make divisions like "mnonn", "opeqr", "st", Now by taking 2 character substrings "mo" and "pr" from "mnonn" and "opeqr" then "st" is already there so using these two length substrings we can generate t by simple concatenation.
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
from bisect import bisect_left from collections import defaultdict def solve(s, t, b, m) : temp = defaultdict(list) l =  * len(s) for i in range(len(s)) : temp['a'].append(i) low = 0 for i in range(len(t)) : indices = temp['a'] it = bisect_left(indices, low) if it == len(indices) : return False count = indices[it] // b l[count] = l[count] + 1 if l[count] >= m : count += 1 low = count * b else : low = indices[it] + 1 return True s = "mnonnopeqrst" t = "moprst" p = 5 q = 2 print (solve(s, t, p, q))
"mnonnopeqrst", "moprst", 5, 2