- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Check if it is possible to form string B from A under the given constraint in Python

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 −

- temp := an empty map containing empty list for all keys
- l := a list of size same as length of s and fill with 0
- for i in range 0 to size of s, do
- insert i at the end of temp['a']

- low := 0
- for i in range 0 to size of t - 1, do
- indices := temp['a']
- it := index to insert low in the indices list to maintain sorted order
- if it is same as size of indices - 1, then
- return False

- count := quotient of (indices[it] / p)
- l[count] := l[count] + 1
- if l[count] >= q, then
- count := count + 1
- low := count * p

- otherwise,
- low := indices[it] + 1

- return True

## Example

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 = [0] * 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))

## Input

"mnonnopeqrst", "moprst", 5, 2

## Output

True

- Related Articles
- Check if it is possible to create a palindrome string from given N in Python
- Check if it is possible to convert one string into another with given constraints in Python
- Check if it is possible to transform one string to another in Python
- Check if it is possible to create a polygon with a given angle in Python
- Check if it is possible to create a polygon with given n sidess in Python
- Check if is possible to get given sum from a given set of elements in Python
- Check if it is possible to draw a straight line with the given direction cosines in Python
- Is it possible to check if a String only contains ASCII in java?
- Check if it is possible to rearrange a binary string with alternate 0s and 1s in Python
- Check if it is possible to sort the array after rotating it in Python
- Check if it is possible to survive on Island in Python
- Is it possible to extract petroleum from under the sea bed?
- Check if it is possible to reach a number by making jumps of two given length in Python
- Check if right triangle possible from given area and hypotenuse in Python
- Check if characters of a given string can be rearranged to form a palindrome in Python