- 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

Suppose we have two strings source and target, we have to find the minimum number of subsequences of source we can form such that if we concatenate them, it will be same as target. If there is no such result, return -1.

So, if the input is like source = "xyz" target = "xyzyzz", then the output will be 3, as we can concatenate these ["xyz" + "yz" + "z"]

To solve this, we will follow these steps −

- s_size := size of s, t_size := size of t
- concat_count := 0, target_idx := 0
- while target_idx < t_size, do
- source_idx := 0
- temp_index := target_idx
- while source_idx < s_size and target_idx < t_size, do
- if s[source_idx] is same as t[target_idx], then
- target_idx := target_idx + 1

- source_idx := source_idx + 1

- if s[source_idx] is same as t[target_idx], then
- if temp_index is same as target_idx, then
- return -1

- concat_count := concat_count + 1

- return concat_count

Let us see the following implementation to get better understanding −

class Solution: def solve(self, s, t): s_size, t_size = len(s), len(t) concat_count = 0 target_idx = 0 while target_idx < t_size: source_idx = 0 temp_index = target_idx while source_idx < s_size and target_idx < t_size: if s[source_idx] == t[target_idx]: target_idx += 1 source_idx += 1 if temp_index == target_idx: return -1 concat_count += 1 return concat_count ob = Solution() source = "xyz" target = "xyzyzz" print(ob.solve(source, target))

"xyz", "xyzyzz"

3

- Related Questions & Answers
- Program to find number of K-Length sublists whose average is greater or same as target in python
- Program to find number of unique subsequences same as target in C++
- Program to find number of sublists whose sum is given target in python
- Program to find minimum number of buses required to reach final target in python
- Program to find an element in list whose value is same as its frequency in Python
- Program to count number of operations needed to make string as concatenation of same string twice in Python
- Find the Number of Subarrays whose Minimum and Maximum are Same using C++
- Program to find minimum number of increments on subarrays to form a target array in Python
- Program to remove all nodes of a linked list whose value is same as in Python
- Program to find maximum sum of the subsequence, where difference of two values is same as their position difference in Python
- Program to check we can find four elements whose sum is same as k or not in Python
- Program to find size of smallest sublist whose sum at least target in Python
- Program to find higher number with same number of set bits as n in Python?\n
- Program to find minimum distance to the target element using Python
- Program to find minimum number of bricks required to make k towers of same height in Python

Advertisements