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() <= 10^{4}

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Starter code: #Back-end complete function Template for Python 3



class Solution:

    def transform(self, A, B): 

        #code here.

Solutions:
class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		c = [0] * 256
		for i in range(len(A)):
			c[ord(A[i])] += 1
			c[ord(B[i])] -= 1
		for i in range(256):
			if c[i] != 0:
				return -1
		i = len(A) - 1
		j = len(B) - 1
		opr = 0
		while i >= 0:
			if A[i] != B[j]:
				opr += 1
			else:
				j -= 1
			i -= 1
		return opr


class Solution:

	def transform(self, A, B):
		d = dict()
		for ch in A:
			d[ch] = d.get(ch, 0) + 1
		for ch in B:
			d[ch] = d.get(ch, 0) - 1
		if any(d.values()):
			return -1
		j = len(B) - 1
		for ch in A[::-1]:
			if B[j] == ch:
				j -= 1
		return j + 1


from collections import Counter

class Solution:

	def transform(self, A, B):
		count = 0
		a = Counter(A)
		b = Counter(B)
		if a != b:
			return -1
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0 and j >= 0:
			if A[i] != B[j]:
				count += 1
				i -= 1
			else:
				i -= 1
				j -= 1
		return count


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		d = {}
		d = {i: 0 for i in A}
		for i in A:
			if i in d:
				d[i] = d[i] + 1
			else:
				d[i] = 0
		for i in B:
			if i in d:
				d[i] = d[i] - 1
		for i in d:
			if d[i] != 0:
				return -1
		n = len(A)
		i = n - 1
		j = n - 1
		res = 0
		while i >= 0 and j >= 0:
			while i >= 0 and A[i] != B[j]:
				res += 1
				i -= 1
			i -= 1
			j -= 1
		return res


from collections import defaultdict

class Solution:

	def transform(self, A, B):
		d = dict()
		for ch in A:
			d[ch] = d.get(ch, 0) + 1
		for ch in B:
			d[ch] = d.get(ch, 0) - 1
		if any(d.values()):
			return -1
		j = len(B) - 1
		for ch in A[::-1]:
			if B[j] == ch:
				j -= 1
		return j + 1


class Solution:

	def transform(self, A, B):
		n1 = len(A)
		n2 = len(B)
		if n1 != n2:
			return -1
		m = {}
		for i in range(n1):
			if A[i] in m:
				m[A[i]] += 1
			else:
				m[A[i]] = 1
		for i in range(n1):
			if B[i] in m:
				m[B[i]] -= 1
		for key in m:
			if m[key] != 0:
				return -1
		(i, j) = (n1 - 1, n2 - 1)
		res = 0
		while i >= 0 and j >= 0:
			while i >= 0 and A[i] != B[j]:
				res += 1
				i -= 1
			i -= 1
			j -= 1
		return res


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		d = {}
		for i in range(len(A)):
			if A[i] in d:
				d[A[i]] = d[A[i]] + 1
			else:
				d[A[i]] = 1
		for i in range(len(A)):
			if B[i] in d:
				d[B[i]] = d[B[i]] - 1
		for k in d:
			if d[k] != 0:
				return -1
		i = len(A) - 1
		j = len(B) - 1
		r = 0
		while i >= 0 and j >= 0:
			while i >= 0 and A[i] != B[j]:
				r = r + 1
				i = i - 1
			i = i - 1
			j = j - 1
		return r


class Solution:

	def transform(self, A, B):
		d = dict()
		for i in A:
			d[i] = d.get(i, 0) + 1
		for i in B:
			d[i] = d.get(i, 0) - 1
		if any(d.values()):
			return -1
		n = len(B) - 1
		for i in A[::-1]:
			if B[n] == i:
				n -= 1
		return n + 1


class Solution:

	def transform(self, A, B):
		d = {}
		for i in A:
			if i in d:
				d[i] = d[i] + 1
			else:
				d[i] = 1
		for i in B:
			if i in d:
				d[i] = d[i] - 1
				if d[i] == 0:
					del d[i]
			else:
				return -1
		if d:
			return -1
		c = len(B) - 1
		for i in A[::-1]:
			if i == B[c]:
				c = c - 1
		return c + 1


class Solution:

	def transform(self, A, B):
		if sorted(A) != sorted(B):
			return -1
		c = 0
		(i, j) = (len(A) - 1, len(B) - 1)
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				c += 1
		return c


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		m1 = {}
		n = len(A)
		for i in range(n):
			if A[i] in m1:
				m1[A[i]] += 1
			else:
				m1[A[i]] = 1
		for i in range(n):
			if B[i] in m1:
				m1[B[i]] -= 1
		for key in m1:
			if m1[key] != 0:
				return -1
		(i, j) = (n - 1, n - 1)
		result = 0
		while i >= 0 and j >= 0:
			while i >= 0 and A[i] != B[j]:
				result += 1
				i -= 1
			i -= 1
			j -= 1
		return result


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		s1 = 0
		s2 = 0
		d1 = {}
		d2 = {}
		for i in A:
			if i not in d1:
				d1[i] = 1
			else:
				d1[i] += 1
		for i in B:
			if i not in d2:
				d2[i] = 1
			else:
				d2[i] += 1
		for i in d1:
			if i not in d2:
				return -1
			if d1[i] != d2[i]:
				return -1
		n = len(A)
		i = n - 1
		j = n - 1
		c = 0
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				c += 1
		return c


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		A1 = {}
		B1 = {}
		n = len(A)
		for i in range(n):
			if A[i] in A1:
				A1[A[i]] += 1
			else:
				A1[A[i]] = 1
			if B[i] in B1:
				B1[B[i]] += 1
			else:
				B1[B[i]] = 1
		if A1 != B1:
			return -1
		result = 0
		i = n - 1
		j = n - 1
		while i >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				result += 1
			i -= 1
			j -= 1
		return result


class Solution:

	def transform(self, A, B):
		res = 0
		if len(A) != len(B) or sorted(A) != sorted(B):
			return -1
		j = len(B) - 1
		for ch in A[::-1]:
			if ch != B[j]:
				res += 1
			else:
				j -= 1
		return res


from collections import Counter

class Solution:

	def transform(self, A, B):
		d1 = dict()
		d2 = dict()
		for ch in A:
			d1[ch] = d1.get(ch, 0) + 1
		for ch in B:
			d2[ch] = d2.get(ch, 0) + 1
		if d1 != d2:
			return -1
		count = 0
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			elif A[i] != B[j]:
				i -= 1
				count += 1
		return count


class Solution:

	def transform(self, A, B):
		d1 = {i: A.count(i) for i in set(A)}
		d2 = {i: B.count(i) for i in set(B)}
		if d1 != d2:
			return -1
		(c, i, j) = (0, len(A) - 1, len(B) - 1)
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			elif A[i] != B[j]:
				i -= 1
				c += 1
		return c


from collections import Counter

class Solution:

	def transform(self, A, B):
		CA = Counter(A)
		CB = Counter(B)
		if sorted(CA.items()) != sorted(CB.items()):
			return -1
		while A:
			if A[-1] == B[-1]:
				B = B[:-1]
			A = A[:-1]
		return len(B)


from collections import Counter

class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		resA = Counter(A)
		resB = Counter(B)
		if resA != resB:
			return -1
		ans = 0
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				ans += 1
		return ans


class Solution:

	def transform(self, A, B):
		(n, m) = (len(A), len(B))
		if n != m:
			return -1
		arr = [0] * 256
		for ch in A:
			arr[ord(ch)] += 1
		for ch in B:
			arr[ord(ch)] -= 1
		for i in arr:
			if i:
				return -1
		ans = 0
		(ch_idx_a, ch_idx_b) = (n - 1, m - 1)
		while ch_idx_a >= 0 and ch_idx_b >= 0:
			if A[ch_idx_a] != B[ch_idx_b]:
				ch_idx_a -= 1
				ans += 1
			else:
				ch_idx_b -= 1
				ch_idx_a -= 1
		return ans


class Solution:

	def transform(self, A, B):
		(n, m) = (len(A), len(B))
		if n != m:
			return -1
		arr = [0] * 256
		for ch in A:
			start = 97 if ch >= 'a' and ch <= 'z' else 65
			arr[ord(ch) - start] += 1
		for ch in B:
			start = 97 if ch >= 'a' and ch <= 'z' else 65
			arr[ord(ch) - start] -= 1
		for i in arr:
			if i:
				return -1
		ans = 0
		(ch_idx_a, ch_idx_b) = (n - 1, m - 1)
		while ch_idx_a >= 0 and ch_idx_b >= 0:
			if A[ch_idx_a] != B[ch_idx_b]:
				ch_idx_a -= 1
				ans += 1
			else:
				ch_idx_b -= 1
				ch_idx_a -= 1
		return ans


class Solution:

	def transform(self, A, B):
		n1 = len(A)
		n2 = len(B)
		if n1 != n2:
			return -1
		d = {}
		for ch in A:
			d.setdefault(ch, 0)
			d[ch] += 1
		for ch in B:
			if ch not in d:
				return -1
			else:
				d[ch] -= 1
		for key in d:
			if d[key] != 0:
				return -1
		i = n1 - 1
		j = n2 - 1
		res = 0
		while i >= 0 and j >= 0:
			if A[i] != B[j]:
				res += 1
				i -= 1
			else:
				i -= 1
				j -= 1
		return res


class Solution:

	def transform(self, A, B):
		dict1 = {}
		dict2 = {}
		count = 0
		for i in A:
			if i in dict1.keys():
				dict1[i] += 1
			else:
				dict1[i] = 1
		for i in B:
			if i in dict2.keys():
				dict2[i] += 1
			else:
				dict2[i] = 1
		if dict1 != dict2:
			return -1
		k = len(A) - 1
		l = k
		res = 0
		while k >= 0:
			while k >= 0 and A[k] != B[l]:
				k -= 1
				res += 1
			if A[k] == B[l]:
				k -= 1
				l -= 1
		return res


class Solution:

	def transform(self, a, b):
		n = len(a)
		cnt = 0
		arr = [0 for i in range(256)]
		for i in range(len(a)):
			arr[ord(a[i])] += 1
		for j in range(len(b)):
			arr[ord(b[j])] -= 1
		sm = 0
		for i in range(256):
			if arr[i] != 0:
				sm = 1
		if sm == 0:
			i = n - 1
			j = n - 1
			while i >= 0:
				while i >= 0 and a[i] != b[j]:
					i = i - 1
					cnt += 1
				if i >= 0:
					i -= 1
					j -= 1
			return cnt
		return -1


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		m = {}
		for x in A:
			if x in m:
				m[x] += 1
			else:
				m[x] = 1
		for x in B:
			if x not in m:
				return -1
			else:
				m[x] -= 1
		for k in m:
			if m[k] != 0:
				return -1
		i = len(A) - 1
		j = i
		step = 0
		while i >= 0 and j >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				step += 1
			i -= 1
			j -= 1
		return step


class Solution:

	def transform(self, A, B):
		d1 = {}
		if len(A) != len(B):
			return -1
		for i in A:
			d1[i] = d1.get(i, 0) + 1
		for i in B:
			d1[i] = d1.get(i, 0) - 1
			if d1[i] == -1:
				return -1
		i = len(A) - 1
		j = 0
		k = len(B) - 1
		while i >= 0:
			if B[k] == A[i]:
				j += 1
				k -= 1
			i -= 1
		return k + 1


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		hmapA = {}
		hmapB = {}
		n = len(A)
		for i in range(n):
			if A[i] in hmapA:
				hmapA[A[i]] += 1
			else:
				hmapA[A[i]] = 1
			if B[i] in hmapB:
				hmapB[B[i]] += 1
			else:
				hmapB[B[i]] = 1
		if hmapA != hmapB:
			return -1
		result = 0
		i = n - 1
		j = n - 1
		while i >= 0:
			while i >= 0 and A[i] != B[j]:
				result += 1
				i -= 1
			i -= 1
			j -= 1
		return result


from collections import Counter

class Solution:

	def transform(self, A, B):
		n = len(A)
		m = len(B)
		if n != m:
			return -1
		hmap1 = {i: j for (i, j) in Counter(A).items()}
		hmap2 = {i: j for (i, j) in Counter(B).items()}
		for (i, j) in hmap1.items():
			if i in hmap2:
				if j != hmap2[i]:
					return -1
			else:
				return -1
		(i, j, c) = (n - 1, n - 1, 0)
		while i >= 0:
			if A[i] == B[j]:
				j -= 1
			else:
				c += 1
			i -= 1
		return c


class Solution:

	def transform(self, a, b):
		n = len(a)
		m = len(b)
		if n != m:
			return -1
		count = 0
		mem = {}
		for c in a:
			if c in mem:
				mem[c] += 1
			else:
				mem[c] = 1
		for c in b:
			if c in mem:
				mem[c] -= 1
			else:
				return -1
		for key in mem:
			if mem[key] != 0:
				return -1
		ai = n - 1
		for i in range(n - 1, -1, -1):
			while ai >= 0 and b[i] != a[ai]:
				count += 1
				ai -= 1
			if ai < 0:
				break
			else:
				ai -= 1
		return count


class Solution:

	def transform(self, A, B):
		m = len(A)
		n = len(B)
		if n != m:
			return -1
		count = [0] * 256
		for i in range(n):
			count[ord(B[i])] += 1
		for i in range(n):
			count[ord(A[i])] -= 1
		for i in range(256):
			if count[i]:
				return -1
		res = 0
		i = n - 1
		j = n - 1
		while i >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				res += 1
			if i >= 0:
				i -= 1
				j -= 1
		return res


from collections import Counter

class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		c = Counter()
		for a in A:
			c[a] += 1
		for b in B:
			c[b] -= 1
			if c[b] < 0:
				return -1
		n = len(A)
		i = n - 1
		j = n - 1
		while i >= 0:
			if A[i] != B[j]:
				i -= 1
			else:
				i -= 1
				j -= 1
		return j + 1


class Solution:

	def transform(self, A, B):
		len_a = len(A)
		len_b = len(B)
		if len_a != len_b:
			return -1
		count = [0 for i in range(256)]
		for i in range(len_a):
			count[ord(A[i])] += 1
			count[ord(B[i])] -= 1
		for i in range(256):
			if count[i] > 0:
				return -1
		start = len_a - 1
		end = len_b - 1
		result = 0
		while start >= 0 and end >= 0:
			if A[start] != B[end]:
				result += 1
				start -= 1
			else:
				start -= 1
				end -= 1
		return result


from collections import Counter

class Solution:

	def transform(self, A, B):
		c = 0
		if A == B:
			return 0
		if len(A) != len(B):
			return -1
		c1 = Counter(A)
		c2 = Counter(B)
		for i in c1:
			if c1[i] != c2[i]:
				return -1
		if A == B:
			return 0
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0 and j >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				c += 1
			i -= 1
			j -= 1
		return c


class Solution:

	def transform(self, A, B):
		da = {}
		db = {}
		na = len(A)
		nb = len(B)
		for i in A:
			da[i] = da.get(i, 0) + 1
		for i in B:
			db[i] = db.get(i, 0) + 1
		if da != db or na != nb:
			return -1
		else:
			(i, j) = (na - 1, nb - 1)
			ans = 0
			while i >= 0:
				if A[i] == B[j]:
					i -= 1
					j -= 1
				else:
					i -= 1
					ans += 1
			return ans


class Solution:

	def transform(self, a, b):
		if len(a) != len(b):
			return -1
		if sorted(a) != sorted(b):
			return -1
		i = len(a) - 1
		j = len(b) - 1
		result = 0
		while i >= 0:
			if a[i] == b[j]:
				i -= 1
				j -= 1
			elif a[i] != b[j]:
				i -= 1
				result += 1
		return result


class Solution:

	def transform(self, s1, s2):
		if len(s1) != len(s2):
			return -1
		dict1 = {}
		dict2 = {}
		for i in s1:
			if i in dict1:
				dict1[i] += 1
			else:
				dict1[i] = 1
		for i in s2:
			if i in dict2:
				dict2[i] += 1
			else:
				dict2[i] = 1
		if dict1 != dict2:
			return -1
		i = len(s2) - 1
		j = len(s1) - 1
		count = 0
		while i >= 0 and j >= 0:
			if ord(s2[i]) == ord(s1[j]):
				j -= 1
				i -= 1
			else:
				j -= 1
				count += 1
		return count


class Solution:

	def transform(self, A, B):
		(i, j) = (len(A) - 1, len(B) - 1)
		(ah, bh) = (0, 0)
		count = [0 for k in range(256)]
		for k in range(len(A)):
			count[ord(A[k])] += 1
		for k in range(len(B)):
			count[ord(B[k])] -= 1
		for k in count:
			if k:
				return -1
		if ah != bh:
			return -1
		c = 0
		while i >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				c += 1
		return c


class Solution:

	def transform(self, A, B):
		count = 0
		a = sorted(A)
		b = sorted(B)
		if a != b:
			return -1
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				count += 1
		return count


class Solution:

	def transform(self, A, B):
		dict1 = {}
		dict2 = {}
		count = 0
		for i in A:
			if i not in dict1:
				dict1[i] = 1
			else:
				dict1[i] += 1
		for j in B:
			if j not in dict2:
				dict2[j] = 1
			else:
				dict2[j] += 1
		if dict1 != dict2:
			return -1
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0 and j >= 0:
			if A[i] != B[j]:
				count += 1
				i -= 1
			else:
				j -= 1
				i -= 1
		return count


class Solution:

	def transform(self, A, B):
		if A == B:
			return 0
		if len(A) != len(B):
			return -1
		dict1 = {}
		dict2 = {}
		(sum1, sum2) = (0, 0)
		for e in A:
			if e not in B:
				return -1
		for c in A:
			sum1 += ord(c)
		for d in B:
			sum2 += ord(d)
		if sum1 != sum2:
			return -1
		count = 0
		i = j = len(A) - 1
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				count += 1
		return count


from collections import Counter

class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		if Counter(A) != Counter(B):
			return -1
		(ct, m, n) = (0, len(A) - 1, len(B) - 1)
		while m >= 0:
			if A[m] == B[n]:
				m -= 1
				n -= 1
			else:
				ct += 1
				m -= 1
		return ct


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		d = [0] * 260
		n = len(A)
		for i in range(n):
			d[ord(A[i]) - ord('a')] += 1
			d[ord(B[i]) - ord('a')] -= 1
		for i in range(260):
			if d[i] != 0:
				return -1
		res = 0
		a = b = n - 1
		while a >= 0:
			if A[a] == B[b]:
				a -= 1
				b -= 1
			else:
				a -= 1
		return b + 1


class Solution:

	def transform(self, A, B):
		countHash = {}
		for i in A + B:
			if i not in countHash or countHash[i] == 0:
				countHash[i] = 1
			else:
				countHash[i] -= 1
		for i in countHash:
			if countHash[i] != 0:
				return -1
		count = 0
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				count += 1
			if i >= 0:
				i -= 1
				j -= 1
		return count


class Solution:

	def transform(self, A, B):
		l1 = len(A) - 1
		l2 = len(B) - 1
		c = 0
		if l1 != l2:
			return -1
		dica = {}
		for i in A:
			if i in dica:
				dica[i] = dica[i] + 1
			else:
				dica[i] = 1
		dicb = {}
		for i in B:
			if i in dicb:
				dicb[i] = dicb[i] + 1
			else:
				dicb[i] = 1
		if dica != dicb:
			return -1
		while l1 >= 0 and l2 >= 0:
			if A[l1] == B[l2]:
				l1 = l1 - 1
				l2 = l2 - 1
			else:
				l1 = l1 - 1
				c = c + 1
		return c


class Solution(object):

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		count = [0] * 256
		for char in A:
			count[ord(char)] += 1
		for char in B:
			count[ord(char)] -= 1
		for val in count:
			if val:
				return -1
		i = j = len(A) - 1
		count = 0
		while i >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				count += 1
			if i >= 0:
				i -= 1
				j -= 1
		return count


class Solution:

	def transform(self, A, B):
		m = len(A)
		n = len(B)
		if n != m:
			return -1
		count = {}
		keys = count.keys()
		for i in A:
			if i in keys:
				count[i] += 1
			else:
				count[i] = 1
		for i in B:
			if i in keys:
				count[i] -= 1
			else:
				count[i] = 1
		for i in keys:
			if count[i]:
				return -1
		res = 0
		i = n - 1
		j = n - 1
		while i >= 0:
			while i >= 0 and A[i] != B[j]:
				i -= 1
				res += 1
			if i >= 0:
				i -= 1
				j -= 1
		return res


class Solution:

	def transform(self, A, B):
		if len(B) != len(A):
			return -1
		hashmap = {}
		(i, j) = (len(A) - 1, len(B) - 1)
		count = 0
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				if A[i] in hashmap:
					hashmap[A[i]] += 1
				else:
					hashmap[A[i]] = 1
				i -= 1
		while j >= 0:
			if B[j] in hashmap and hashmap[B[j]] > 0:
				hashmap[B[j]] -= 1
				j -= 1
				count += 1
			else:
				return -1
		return count


from collections import Counter

class Solution:

	def transform(self, arr1, arr2):
		m = len(arr1)
		n = len(arr2)
		if m != n or Counter(arr1) != Counter(arr2):
			return -1
		i = m - 1
		j = n - 1
		count = 0
		while i >= 0 and j >= 0:
			if arr1[i] == arr2[j]:
				j -= 1
				i -= 1
			else:
				count += 1
				i -= 1
		return count


from collections import Counter

class Solution:

	def transform(self, A, B):
		n = Counter(A)
		m = Counter(B)
		ctr = 0
		if n == m:
			m = len(A)
			n = len(B)
			if n != m:
				return -1
			count = [0] * 256
			for i in range(n):
				count[ord(B[i])] += 1
			for i in range(n):
				count[ord(A[i])] -= 1
			for i in range(256):
				if count[i]:
					return -1
			res = 0
			i = n - 1
			j = n - 1
			while i >= 0:
				while i >= 0 and A[i] != B[j]:
					i -= 1
					res += 1
				if i >= 0:
					i -= 1
					j -= 1
			return res
		else:
			return -1
		return ctr


class Solution:

	def transform(self, A, B):
		a = len(A)
		b = len(B)
		if a != b:
			return -1
		suma = sumb = 0
		for i in A:
			suma += ord(i)
		for i in B:
			sumb += ord(i)
		if suma != sumb:
			return -1
		dict = {}
		for i in A:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		for j in B:
			if j in dict:
				dict[j] -= 1
			else:
				return -1
		i = a - 1
		j = b - 1
		count = 0
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				count += 1
				i -= 1
		return count


class Solution:

	def transform(self, a, b):
		m = len(a)
		n = len(b)
		if m != n:
			return -1
		countchar = [0] * 256
		for i in range(m):
			countchar[ord(a[i])] += 1
		for i in range(m):
			countchar[ord(b[i])] -= 1
		for i in range(256):
			if countchar[i]:
				return -1
		i = m - 1
		j = m - 1
		res = 0
		while i >= 0:
			while i >= 0 and a[i] != b[j]:
				i -= 1
				res += 1
			if i >= 0:
				i -= 1
				j -= 1
		return res


class Solution:

	def transform(self, A, B):
		if len(A) != len(B):
			return -1
		d1 = [0] * 256
		for i in range(len(A)):
			d1[ord(A[i])] += 1
			d1[ord(B[i])] -= 1
		for i in d1:
			if i != 0:
				return -1
		res = 0
		j = len(A) - 1
		for i in range(1, len(A) + 1):
			if B[j] != A[-i]:
				res += 1
			else:
				j -= 1
		return res


class Solution:

	def transform(self, A, B):
		l1 = len(A)
		l2 = len(B)
		i = l1 - 1
		j = l2 - 1
		c = 0
		a1 = 0
		a2 = 0
		s1 = ''.join(sorted(A))
		s2 = ''.join(sorted(B))
		if s1 != s2:
			return -1
		for i in range(l1):
			a1 = a1 + ord(A[i])
		for i in range(l2):
			a2 = a2 + ord(B[i])
		if a1 != a2:
			return -1
		if l1 != l2:
			return -1
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i = i - 1
				j = j - 1
			else:
				i = i - 1
				c = c + 1
		return c


class Solution:

	def transform(self, A, B):
		res = 0
		if len(A) != len(B):
			return -1
		c_a = [0] * 256
		for i in B:
			c_a[ord(i)] += 1
		for i in A:
			c_a[ord(i)] -= 1
		for i in c_a:
			if i != 0:
				return -1
		i = len(A) - 1
		j = len(B) - 1
		while i >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			while i >= 0 and A[i] != B[j]:
				res += 1
				i -= 1
		return res


class Solution:

	def transform(self, A, B):
		d = dict()
		if len(A) != len(B):
			return -1
		for i in A:
			if i not in d:
				d[i] = 1
			else:
				d[i] = d[i] + 1
		for i in B:
			if i not in d:
				return -1
			else:
				d[i] = d[i] - 1
		for i in d.values():
			if i != 0:
				return -1
		i = len(A) - 1
		j = len(B) - 1
		ans = 0
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i = i - 1
				j = j - 1
			else:
				i = i - 1
				ans = ans + 1
		return ans


class Solution:

	def transform(self, A, B):
		a1 = A
		b1 = B
		if sorted(a1) != sorted(b1):
			return -1
		i = len(A) - 1
		j = len(B) - 1
		total = 0
		while i >= 0 and j >= 0:
			if A[i] == B[j]:
				i -= 1
				j -= 1
			else:
				i -= 1
				total += 1
		return total


from collections import Counter

class Solution:

	def transform(self, a, b):
		count = 0
		if Counter(a) != Counter(b) or len(a) != len(b):
			return -1
		i = len(a) - 1
		j = len(b) - 1
		while i >= 0:
			if a[i] == b[j]:
				i -= 1
				j -= 1
			else:
				count += 1
				i -= 1
		return count


class Solution:

	def transform(self, A, B):
		i_pointer = len(A) - 1
		j_pointer = len(B) - 1
		count = 0
		hm_A = {}
		hm_B = {}
		for i in B:
			if i not in hm_B:
				hm_B[i] = 1
			else:
				hm_B[i] += 1
		for j in A:
			if j not in hm_A:
				hm_A[j] = 1
			else:
				hm_A[j] += 1
		while i_pointer >= 0:
			if A[i_pointer] == B[j_pointer]:
				if hm_A[A[i_pointer]] == hm_B[B[j_pointer]]:
					i_pointer -= 1
					j_pointer -= 1
				else:
					return -1
			else:
				while A[i_pointer] != B[j_pointer] and i_pointer >= 0:
					if A[i_pointer] not in hm_B or B[j_pointer] not in hm_A:
						return -1
					elif hm_A[A[i_pointer]] != hm_B[A[i_pointer]] or hm_B[B[j_pointer]] != hm_A[B[j_pointer]]:
						return -1
					else:
						count += 1
						i_pointer -= 1
		return count