Transform String | Practice | GeeksforGeeks

Given two strings A and B. Find the minimum number of steps required to transform string A into string B. The only allowed operation for the transformation is selecting a character from string A and inserting it in the beginning of string A.
Example 1:
Input:
A = "abd"
B = "bad"
Output: 1
Explanation: The conversion can take place in
1 operation: Pick 'b' and place it at the front.
Example 2:
Input:
A = "GeeksForGeeks"
B = "ForGeeksGeeks"
Output: 3
Explanation: The conversion can take
place in 3 operations:
Pick 'r' and place it at the front.
A = "rGeeksFoGeeks"
Pick 'o' and place it at the front.
A = "orGeeksFGeeks"
Pick 'F' and place it at the front.
A = "ForGeeksGeeks"
Your Task:  
You dont need to read input or print anything. Complete the function transform() which takes two strings A and B as input parameters and returns the minimum number of steps required to transform A into B. If transformation is not possible return -1.

Expected Time Complexity: O(N) where N is max(length of A, length of B) 
Expected Auxiliary Space: O(1)  

Constraints:
1<= A.length(), B.length() <= 104
 
0
dipeshmakwane2 weeks ago
  int transform (string A, string B)
    {
        //code here.
        	if(A.length()!=B.length()){
    	    return -1;
    	}
    	int temp1[256]={0};
    	int temp2[256]={0};
    	for(int i=0;i<A.length();i++){
    	    temp1[A[i]]++;
    	    temp2[B[i]]++;
    	}
    	for(int i=0;i<256;i++){
    	    if(temp1[i]!=temp2[i])
    	    return -1;
    	}
    	int count=0;
    	int i=A.length()-1;
    	int j=B.length()-1;
    	while(i>=0){
    	    if(A[i]==B[j]){
    	        i--;
    	        j--;
    	    }
    	    else{
    	        i--;
    	        count++;
    	    }
    	}
    	return count;
    }
0
swapniltayal4222 months ago
class Solution:   def transform(self, A, B):        #code here.       if sorted(A) != sorted(B):           return -1       if len(A) != len(B):           return -1          p1 = len(A) - 1       p2 = len(B) - 1       res = 0       while p1 >= 0:           if A[p1] != B[p2]:               res += 1           else:               p2 -= 1           p1 -= 1       return res
+3
shivangtiwari18023 months ago
C++ Solution “ Time : O(N) && Space O(1) __All test cases passed ”
 int transform (string A, string B)   {       if (A.length() != B.length())       {           return -1;       }       int sum = 0 , sum1 = 0;       int i = A.length() - 1 , j = B.length() - 1;       int ct = 0;       while (i >= 0 && j >= 0)       {           sum += A[i];           sum1 += B[j];           i--;           j--;       }       if (sum1 != sum)       {           return -1;       }       i = A.length() - 1;       j = B.length() - 1;              while (i >= 0 && j >= 0)       {           if (A[i] == B[j])           {               i--;               j--;           }           else           {               ct++;               i--;           }       }       return ct;   }
-1
aaravarya13 months ago
C++ Easy Code (0.1 Sec) Accepted!
 
int transform (string A, string B)   {       //code here.       unordered_map<char,int> uA;       unordered_map<char,int> uB;              for(int i=0; A[i]!='\0'; i++)       uA[A[i]]++;              for(int i=0; B[i]!='\0'; i++)       uB[B[i]]++;              for(auto it: uB)       {           if(uA.find(it.first)==uA.end())           return -1;           if(uA[it.first]!= it.second)           return -1;       }              int lenA= A.size();       int lenB=B.size();              int count=0;       for(int i=lenA-1, j=lenB-1; i>=0 , j>=0; i--, j--)       {           if(A[i]!=B[j])           {               while(A[i]!=B[j]&& i>=0)               {                   i--;                   count++;               }           }       }       return count;   }
-1
sangamchoudhary75 months ago
   int transform (string A, string B){
        int check[256] = {0};
        if(A.length() != B.length()){
            return -1;
        }
        for(int i=0,j=0;i<A.size();i++){
            check[A[i]]++;
            check[B[i]]--;
        }
        for(int i=0;i<256;i++){
            if(check[i] != 0){
                return -1;
            }
        }
        int i = A.size() - 1;
        int j = B.size() - 1;
        int diff = 0;
        while(i>=0 and j>=0){
            if(A[i] == B[j]){
                i--,j--;
            }else{
                diff++;
                i--;
            }
        }
        return diff;
    }
0
riturajrai4406 months ago
if (A.length () != B.length ())       {           return -1;       }          unordered_map <char, int> m;       int n = A.length ();          for (int i = 0; i < n; i++)           m[A[i]]++;          for (int i = 0; i < n; i++)           if (m[B[i]])               m[B[i]]--;          for (auto i : m)       {           if (i.second != 0)           {               return -1;           }       }             int i = n - 1, j = n - 1;       int res = 0;       while (i >= 0 and j >= 0)       {           while (i >= 0 and A[i] != B[j])           {               i--;               res++;           }              i--;           j--;       }          return res;
0
tauruas08057 months ago
if(A.size()!=B.size())return -1;       unordered_map<char,int>m1,m2;int n=A.size();       for(int i=0;i<n;i++){m1[A[i]]++;m2[B[i]]++;}       if(m1!=m2)return -1;       int i=n-1,j=n-1;int ans=0;       while(i>=0 and j>=0){           while(i>=0 and A[i]!=B[j])i--,ans++;           if(i>=0)i--,j--;       }      return ans;
-2
SATYAM7 months ago
SATYAM
class Solution{    int transform (String A, String B)    {        if(A.length()!=B.length())        return -1;        HashMap<character,integer> map=new HashMap<>();        for(int i=0;i<a.length();i++){ if(map.containskey(a.charat(i)))="" map.put(a.charat(i),map.get(a.charat(i))+1);="" else="" map.put(a.charat(i),1);="" }="" for(int="" i="0;i&lt;B.length();i++){" if(map.containskey(b.charat(i))="" &&="" map.get(b.charat(i))="">0)            map.put(B.charAt(i),map.get(B.charAt(i))-1);            else return -1;        }        int count=0;        int i=A.length()-1;        int j=B.length()-1;        while(i>=0 && j>=0){          if(A.charAt(i)==B.charAt(j)){          i--;          j--;          }          else {              i--;              count++;          }        }        return count;    }}
0
Vaibhav  Bhatnagar8 months ago
Vaibhav  Bhatnagar
class Solution{    int transform (String A, String B)    {        if(A.length()!=B.length())        return -1;        int []arr=new int[256];        for(int i=0;i<a.length();i++) arr[a.charat(i)]++;="" for(int="" i="0;i&lt;A.length();i++)" arr[b.charat(i)]--;="" for(int="" i="0;i&lt;256;i++)" if(arr[i]!="0)" return="" -1;="" int="" i="A.length()-1;" int="" j="A.length()-1;" int="" res="0;" while(i="">=0){            if(A.charAt(i)!=B.charAt(j))            res++;            else            j--;             i--;        }        return res;     }}
0
kartikeya Sharma8 months ago
kartikeya Sharma
int transform (string A, string B)    {                int N = A.size();        int M = B.size();        int hash1[256] = {0};int hash2[256] = {0};        if(N != M) return -1;        for(int i = 0; i < N; i++) hash1[A[i]]++;        for(int i = 0; i < M; i++) hash2[B[i]]++;        for(int i = 0; i < 256; i++) if(hash1[i] != hash2[i]) return -1;       int i = N-1;       int j = M-1;        while(i>=0 && j>=0)        {            if(A[i] == B[j])                 {                     i--;                    j--;                }            else i--;           }        return j+1;    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.