- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find number of ways to form a target string given a dictionary in Python

Suppose we have a list of string called words, where all elements are of same length. We also have a string called target. We have to generate target using the given words under the following rules −

We should generate target from left to right.

To get the ith character (0-indexed) of target, we can select the kth character of the jth string in words when target[i] is same as words[j][k].

Once we use the kth character of the jth string of words, we cannot use the xth character of any string in words where x <= k.

Repeat these process until we form the entire target string.

So we have to find the number of ways to get target from words. The answer may be very large, so return answer modulo 10^9 + 7.

So, if the input is like words = ["pqqp","qppq"], target = "qpq", then the output will be 4 because

"qpq" -> at index 0 ("qppq"), at index 1 ("qppq"), at index 2 ("pqqp")

"qpq" -> at index 0 ("qppq"), at index 1 ("qppq"), at index 3 ("qppq")

"qpq" -> at index 0 ("qppq"), at index 2 ("qppq"), at index 3 ("qppq")

"qpq" -> at index 1 ("pqqp"), at index 2 ("qppq"), at index 3 ("qppq")

To solve this, we will follow these steps −

m := number of words,

n := size of target

d := a list of size m, filled with m different empty maps

for each w in words, do

for each index j and word c in w, do

d[j, c] := d[j, c] + 1

Define a function dfs() . This will take i, j

if i is same as n, then

return 1

if i is same as m, then

return 0

return (dfs(i, j+1) + dfs(i+1, j+1) * d[j, target[i]]) mod (10^9 + 7)

From the main method return dfs(0, 0)

## Example

Let us see the following implementation to get better understanding

from collections import Counter def solve(words, target): m, n = len(words[0]), len(target) d = [Counter() for _ in range(m)] for w in words: for j, c in enumerate(w): d[j][c] += 1 def dfs(i, j): if i == n: return 1 if j == m: return 0 return (dfs(i, j+1) + dfs(i+1, j+1) * d[j][target[i]]) % int(1e9 + 7) return dfs(0, 0) words = ["pqqp","qppq"] target = "qpq" print(solve(words, target))

## Input

"95643", "45963"

## Output

4

- Related Questions & Answers
- Program to find number of ways to split a string in Python
- Program to find minimum number of increments on subarrays to form a target array in Python
- Program to find number of good ways to split a string using Python
- Program to find number of ways we can arrange symbols to get target in Python?
- Program to find number of given operations required to reach Target in Python
- Program to find number of sublists whose sum is given target in python
- Python - Ways to create a dictionary of Lists
- Python Program to Form a Dictionary from an Object of a Class
- Program to find out number of distinct substrings in a given string in python
- C++ Program to Find the Number of Permutations of a Given String
- Program to find a good string from a given string in Python
- Program to find number of ways we can decode a message in Python
- Program to find number of ways we can split a palindrome in python
- Program to find a target value inside given matrix or not in Python
- How to translate a python string according a given dictionary?