Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //{ Driver Code Starts
- #include <bits/stdc++.h>
- #include <unordered_map>
- using namespace std;
- // } Driver Code Ends
- class Solution
- {
- public:
- int transform (string A, string B)
- {
- //code here.
- int n=A.length();
- int m=B.length();
- if(n!=m)
- {
- return -1;
- }
- else
- {
- unordered_map<char,int> mp;
- for(int i=0;i<n;i++)
- {
- mp[A[i]]++;
- }
- for(int i=0;i<n;i++)
- {
- mp[B[i]]--;
- }
- for(auto u:mp)
- {
- if(u.second!=0)
- {
- return -1;
- }
- }
- int i=n-1,j=m-1;
- int c=0;
- while(i>=0 && j>=0)
- {
- if(A[i]==B[j])
- {
- i--;
- j--;
- }
- else
- {
- c++;
- i--;
- }
- }
- return c;
- }
- }
- };
- //{ Driver Code Starts.
- int main ()
- {
- int t; cin >> t;
- while (t--)
- {
- string A, B;
- cin >> A >> B;
- Solution ob;
- cout <<ob.transform (A, B) << endl;
- }
- }
- // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement