Check if it is possible to form string B from A under the given constraints
Given two strings A and B and two integers b and m. The task is to find that if it is possible to form string B from A such that A is divided into groups of b characters except the last group which will have characters ≤ b and you are allowed to pick atmost m characters from each group, and also order of characters in B must be same as that of A. If it is possible then print Yes else print No.
Input: A = abcbbcdefxyz, B = acdxyz, b = 5, m = 2
Groups can be “abcbb”, “cdefx” and “yz”
Now “acdxyz” can be used to pick “ac” and “dx” can be picked from “cdefx”.
Finally, “yz” if the last group.
Input: A = abcbbcdefxyz, B = baz, b = 3, m = 2
Approach: The idea is to use binary search. Iterate through string A and store the frequency of each of the characters of A in vector S. Now iterate through B and if the current character is not in the vector then print No since its not possible to form string B using A. Else, check the first occurrence of current character starting from the index of the last chosen character low, which denotes starting position in string A from where we want to match characters of string B. Keep track of number of characters stored in each group. If it exceeds, the given limit of characters in current block, we update the pointer low to the next block.
Below is the implementation of the above approach: