Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum number of subsequence whose concatenation is same as target in python
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"]
Algorithm
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
Example
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))
3
How It Works
The algorithm works by greedily matching characters from the source string to form subsequences:
- For each pass through the source string, we try to match as many characters as possible with the remaining target characters
- If no characters are matched in a complete pass through source, it means we cannot form the target (return -1)
- Otherwise, we increment the subsequence count and continue with the remaining target characters
Test with Different Examples
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()
# Test case 1: Normal case
print("Test 1:", ob.solve("xyz", "xyzyzz"))
# Test case 2: Impossible case
print("Test 2:", ob.solve("abc", "def"))
# Test case 3: Single character repeated
print("Test 3:", ob.solve("a", "aaaa"))
Test 1: 3 Test 2: -1 Test 3: 4
Conclusion
This greedy approach efficiently finds the minimum number of subsequences needed by maximizing the characters matched in each pass through the source string. The time complexity is O(n*m) where n is the length of target and m is the length of source.
