Advertisement
jayati

Transform String

Sep 29th, 2023
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. #include <unordered_map>
  4. using namespace std;
  5.  
  6.  
  7. // } Driver Code Ends
  8. class Solution
  9. {
  10.     public:
  11.     int transform (string A, string B)
  12.     {
  13.         //code here.
  14.         int n=A.length();
  15.         int m=B.length();
  16.         if(n!=m)
  17.         {
  18.             return -1;
  19.         }
  20.         else
  21.         {
  22.             unordered_map<char,int> mp;
  23.            
  24.             for(int i=0;i<n;i++)
  25.             {
  26.                 mp[A[i]]++;
  27.             }
  28.             for(int i=0;i<n;i++)
  29.             {
  30.                 mp[B[i]]--;
  31.             }
  32.            
  33.             for(auto u:mp)
  34.             {
  35.                 if(u.second!=0)
  36.                 {
  37.                     return -1;
  38.                 }
  39.             }
  40.            
  41.              int i=n-1,j=m-1;
  42.                int c=0;
  43.                while(i>=0 && j>=0)
  44.                {
  45.                    if(A[i]==B[j])
  46.                    {
  47.                        i--;
  48.                        j--;
  49.                    }
  50.                    else
  51.                    {
  52.                        c++;
  53.                        i--;
  54.                    }
  55.                }
  56.                return c;
  57.            
  58.         }
  59.     }
  60. };
  61.  
  62.  
  63. //{ Driver Code Starts.
  64.  
  65. int main ()
  66. {
  67.     int t; cin >> t;
  68.     while (t--)
  69.     {
  70.         string A, B;
  71.         cin >> A >> B;
  72.         Solution ob;
  73.         cout <<ob.transform (A, B) << endl;
  74.     }
  75. }
  76. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement