给定两个仅由小写字母组成的字符串A和B ,任务是找到从 A 形成 B 所需的最小子序列数。
例子:
Input: A = “abbace” B = “acebbaae”
Output: 3
Explanation:
Sub-sequences “ace”, “bba”, “ae” from string A used to form string B
Input: A = “abc” B = “cbacbacba”
Output: 7
方法:
- 为A 的每个字符维护一个数组,该数组将按递增顺序存储其索引。
- 遍历B 的每个元素并在需要新子序列时增加计数器。
- 维护一个变量minIndex ,它将显示可以在当前子序列中采用大于该索引的元素,否则增加计数器并将 minIndex 更新为 -1。
下面是上述方法的实现。
C++
// C++ program to find the Minimum number
// of subsequences required to convert
// one string to another
#include
using namespace std;
// Function to find the no of subsequences
int minSubsequnces(string A, string B)
{
vector v[26];
int minIndex = -1, cnt = 1, j = 0;
int flag = 0;
for (int i = 0; i < A.length(); i++) {
// Push the values of indexes of each character
int p = (int)A[i] - 97;
v[p].push_back(i);
}
while (j < B.length()) {
int p = (int)B[j] - 97;
// Find the next index available in the array
int k = upper_bound(v[p].begin(),
v[p].end(), minIndex)
- v[p].begin();
// If Character is not in string A
if (v[p].size() == 0) {
flag = 1;
break;
}
// Check if the next index is not equal to the
// size of array which means there is no index
// greater than minIndex in the array
if (k != v[p].size()) {
// Update value of minIndex with this index
minIndex = v[p][k];
j = j + 1;
}
else {
// Update the value of counter
// and minIndex for next operation
cnt = cnt + 1;
minIndex = -1;
}
}
if (flag == 1) {
return -1;
}
return cnt;
}
// Driver Code
int main()
{
string A1 = "abbace";
string B1 = "acebbaae";
cout << minSubsequnces(A1, B1) << endl;
return 0;
}
Python3
# Python3 program to find the Minimum number
# of subsequences required to convert
# one to another
from bisect import bisect as upper_bound
# Function to find the no of subsequences
def minSubsequnces(A, B):
v = [[] for i in range(26)]
minIndex = -1
cnt = 1
j = 0
flag = 0
for i in range(len(A)):
# Push the values of indexes of each character
p = ord(A[i]) - 97
v[p].append(i)
while (j < len(B)):
p = ord(B[j]) - 97
# Find the next index available in the array
k = upper_bound(v[p], minIndex)
# If Character is not in A
if (len(v[p]) == 0):
flag = 1
break
# Check if the next index is not equal to the
# size of array which means there is no index
# greater than minIndex in the array
if (k != len(v[p])):
# Update value of minIndex with this index
minIndex = v[p][k]
j = j + 1
else:
# Update the value of counter
# and minIndex for next operation
cnt = cnt + 1
minIndex = -1
if (flag == 1):
return -1
return cnt
# Driver Code
A1 = "abbace"
B1 = "acebbaae"
print(minSubsequnces(A1, B1))
# This code is contributed by mohit kumar 29
输出:
3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。