# 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**.

**Examples:**

Input:A = abcbbcdefxyz, B = acdxyz, b = 5, m = 2

Output:Yes

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

Output:No

**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:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if it is possible ` `// to form B from A satisfying the given conditions ` `bool` `isPossible(string A, string B, ` `int` `b, ` `int` `m) ` `{ ` ` ` ` ` `// Vector to store the frequency ` ` ` `// of characters in A ` ` ` `vector<` `int` `> S[26]; ` ` ` ` ` `// Vector to store the count of characters ` ` ` `// used from a particular group of characters ` ` ` `vector<` `int` `> box(A.length(), 0); ` ` ` ` ` `// Store the frequency of the characters ` ` ` `for` `(` `int` `i = 0; i < A.length(); i++) { ` ` ` `S[A[i] - ` `'a'` `].push_back(i); ` ` ` `} ` ` ` ` ` `int` `low = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < B.length(); i++) { ` ` ` `auto` `it = lower_bound(S[B[i] - ` `'a'` `].begin(), ` ` ` `S[B[i] - ` `'a'` `].end(), low); ` ` ` ` ` `// If a character in B is not ` ` ` `// present in A ` ` ` `if` `(it == S[B[i] - ` `'a'` `].end()) ` ` ` `return` `false` `; ` ` ` ` ` `int` `count = (*it) / b; ` ` ` `box[count] = box[count] + 1; ` ` ` ` ` `// If count of characters used from ` ` ` `// a particular group of characters ` ` ` `// exceeds m ` ` ` `if` `(box[count] >= m) { ` ` ` `count++; ` ` ` ` ` `// Update low to the starting index ` ` ` `// of the next group ` ` ` `low = (count)*b; ` ` ` `} ` ` ` ` ` `// If count of characters used from ` ` ` `// a particular group of characters ` ` ` `// has not exceeded m ` ` ` `else` ` ` `low = (*it) + 1; ` ` ` `} ` ` ` ` ` `return` `true` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string A = ` `"abcbbcdefxyz"` `; ` ` ` `string B = ` `"acdxyz"` `; ` ` ` `int` `b = 5; ` ` ` `int` `m = 2; ` ` ` ` ` `if` `(isPossible(A, B, b, m)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Yes

## Recommended Posts:

- Construct a binary string following the given constraints
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Check if a string contains a palindromic sub-string of even length
- Divide array into two parts with equal sum according to the given constraints
- Find minimum time to finish all jobs with given constraints
- Check if a string is suffix of another
- Check if the given string is K-periodic
- Check if the given string is the same as its reflection in a mirror
- Minimum time required to transport all the boxes from source to the destination under the given constraints
- Check if given string can be formed by two other strings or their permutations
- Check if a binary string has two consecutive occurrences of one everywhere
- Convert given integer X to the form 2^N - 1
- Count of integers of the form (2^x * 3^y) in the range [L, R]
- Arrange given numbers to form the smallest number
- Print all triplets in sorted array that form AP

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.