Count characters to be shifted from the start or end of a string to obtain another string
Last Updated :
18 Jan, 2024
Given two strings A and B where string A is an anagram of string B . In one operation, remove the first or the last character of the string A and insert at any position in A . The task is to find the minimum number of such operations required to be performed to convert string A into string B .
Examples:
Input: A = “edacb”, B = “abcde” Output : 3 Explanation: Perform the given operations as follows: Step1: Take last character ‘b’ and insert it between ‘a’ and ‘c’ ( “edacb” becomes “edabc” ) Step2: Take first character ‘e’ insert it to last ( “edabc” becomes “dabce” ) Step3: Take first character ‘d’ and insert it between ‘c’ and ‘e’ ( “dabce” becomes “abcde” ) Therefore, the count of the operations is 3.
Input: A = “abcd”, B = “abdc” Output: 1 Explanation: Perform the given operations as follows: Step1: Take last character ‘d’ and insert it between ‘b’ and ‘c’ ( “abcd” becomes “abdc” ) Therefore, the count of the operations is 1.
Approach: The idea is to find the longest substring of string A which is also a subsequence of string B , and subtract this value from the given length of string to get the minimum count of operation required.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int minCost(string A, string B)
{
int n = A.size();
int i = 0;
int maxlen = 0;
while (i < n)
{
int length = 0;
for(int j = 0; j < n; ++j)
{
if (A[i] == B[j])
{
++i;
++length;
if (i == n)
break;
}
}
maxlen = max(maxlen,
length);
}
return n - maxlen;
}
int main()
{
string A = "edacb";
string B = "abcde";
cout << minCost(A, B) << endl;
}
Java
import java.util.*;
import java.lang.*;
class GFG {
static int minCost(String A, String B)
{
int n = A.length();
int i = 0;
int maxlen = 0;
while (i < n) {
int length = 0;
for (int j = 0; j < n; ++j) {
if (A.charAt(i) == B.charAt(j)) {
++i;
++length;
if (i == n)
break;
}
}
maxlen = Math.max(maxlen,
length);
}
return n - maxlen;
}
public static void main(String[] args)
{
String A = "edacb";
String B = "abcde";
System.out.println(minCost(A, B));
}
}
Python3
def minCost(A, B):
n = len(A);
i = 0;
maxlen = 0;
while (i < n):
length = 0;
for j in range(0, n):
if (A[i] == B[j]):
i+= 1
length+=1;
if (i == n):
break;
maxlen = max(maxlen, length);
return n - maxlen;
if __name__ == '__main__':
A = "edacb";
B = "abcde";
print(minCost(A, B));
C#
using System;
using System.Collections.Generic;
class GFG{
static int minCost(string A, string B)
{
int n = A.Length;
int i = 0;
int maxlen = 0;
while (i < n)
{
int length = 0;
for(int j = 0; j < n; ++j)
{
if (A[i] == B[j])
{
++i;
++length;
if (i == n)
break;
}
}
maxlen = Math.Max(maxlen,
length);
}
return n - maxlen;
}
public static void Main()
{
string A = "edacb";
string B = "abcde";
Console.WriteLine(minCost(A, B));
}
}
Javascript
<script>
function minCost(A, B)
{
var n = A.length;
var i = 0;
var maxlen = 0;
while (i < n)
{
var length = 0;
for(var j = 0; j < n; ++j)
{
if (A[i] == B[j])
{
++i;
++length;
if (i == n)
break;
}
}
maxlen = Math.max(maxlen,
length);
}
return n - maxlen;
}
var A = "edacb";
var B = "abcde";
document.write(minCost(A, B));
</script>
Time Complexity: O(N 2 ), where N is the length of the strings Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Add Your Comment
Please Login to comment...