Given two strings S1 and S2 in lowercase, the task is to make them anagram. The only allowed operation is to remove a character from any string. Find the minimum number of characters to be deleted to make both the strings anagram. Two strings are called anagram of each other if one of them can be converted into another by rearranging its letters.
Example 1:
Input:
S1 = bcadeh
S2 = hea
Output: 3
Explanation: We need to remove b, c
and d from S1.
Example 2:
Input:
S1 = cddgk
S2 = gcd
Output: 2
Explanation: We need to remove d and
k from S1.
Your Task:
Complete the function remAnagram() which takes two strings S1, S2 as input parameter, and returns minimum characters needs to be deleted.
Expected Time Complexity: O(max(|S1|, |S2|)), where |S| = length of string S.
Expected Auxiliary Space: O(26)
Constraints:
1 <= |S1|, |S2| <= 10^{5}

Difficulty: EASY
Tags: String algorithms

Data structures
Skill types: Data structures
Expected Time Complexity: O(max(|S1|, |S2|)), where |S| = length of string S.
Expected Auxiliary Space: O(26)

Starter code:
# function to calculate minimum numbers of characters
# to be removed to make two strings anagram
def remAnagram(str1,str2):
    
    #add code here
    
    

Solutions:
def remAnagram(S1, S2):
	count_s1 = {}
	count_s2 = {}
	for char in S1:
		count_s1[char] = count_s1.get(char, 0) + 1
	for char in S2:
		count_s2[char] = count_s2.get(char, 0) + 1
	deletions = 0
	for char in 'abcdefghijklmnopqrstuvwxyz':
		count1 = count_s1.get(char, 0)
		count2 = count_s2.get(char, 0)
		deletions += abs(count1 - count2)
	return deletions


def remAnagram(str1, str2):
	dic = {}
	dic1 = {}
	an = 0
	for i in range(26):
		dic[chr(97 + i)] = 0
	for i in range(26):
		dic1[chr(97 + i)] = 0
	for i in str2:
		dic[i] += 1
	for i in str1:
		dic1[i] += 1
	for i in dic:
		an += abs(dic[i] - dic1[i])
	return an


def remAnagram(str1, str2):
	for i in str2:
		if i in str1:
			str1 = str1.replace(i, '', 1)
			str2 = str2.replace(i, '', 1)
	return len(str1) + len(str2)


def remAnagram(str1, str2):
	a = ''
	b = ''
	if len(str1) >= len(str2):
		a = str1
		b = str2
	else:
		a = str2
		b = str1
	(d1, d2) = ({}, {})
	for c in a:
		d1[c] = d1.get(c, 0) + 1
	for c in b:
		d2[c] = d2.get(c, 0) + 1
	keys = set(d2.keys())
	keys.update(d1.keys())
	count = 0
	for c in keys:
		if c in d1 and c in d2 and (d1[c] != d2[c]):
			count += abs(d1[c] - d2[c])
		elif c not in d1:
			count += d2[c]
		elif c not in d2:
			count += d1[c]
	return count


def remAnagram(str1, str2):
	for i in str1:
		if i in str2:
			str1 = str1[:str1.index(i)] + str1[str1.index(i) + 1:]
			str2 = str2[:str2.index(i)] + str2[str2.index(i) + 1:]
	return len(str1) + len(str2)


def remAnagram(str1, str2):
	ind1 = [0] * 26
	ind2 = [0] * 26
	for c in str1:
		ind1[ord(c) - ord('a')] += 1
	for c in str2:
		ind2[ord(c) - ord('a')] += 1
	diff = 0
	for i in range(26):
		diff += abs(ind1[i] - ind2[i])
	return diff


def remAnagram(str1, str2):
	lenStr1 = len(str1)
	lenStr2 = len(str2)
	d = {}
	summ = 0
	if lenStr1 >= lenStr2:
		for i in str1:
			d[i] = d.get(i, 0) + 1
		for i in str2:
			if i not in str1:
				d[i] = d.get(i, 0) + 1
			else:
				d[i] = d[i] - 1
		for i in d.values():
			summ += abs(i)
		return summ
	else:
		for i in str2:
			d[i] = d.get(i, 0) + 1
		for i in str1:
			if i not in str2:
				d[i] = d.get(i, 0) + 1
			else:
				d[i] = d[i] - 1
		for i in d.values():
			summ += abs(i)
		return summ


def remAnagram(str1, str2):
	a = sorted(str1)
	b = sorted(str2)
	map1 = {}
	map2 = {}
	for i in range(len(a)):
		if a[i] in map1:
			map1[a[i]] += 1
		else:
			map1[a[i]] = 1
	for i in range(len(b)):
		if b[i] in map2:
			map2[b[i]] += 1
		else:
			map2[b[i]] = 1
	count = 0
	for (x, y) in map1.items():
		if x in map2:
			count += abs(y - map2[x])
			del map2[x]
		else:
			count += y
	for (x, y) in map2.items():
		count += y
	return count


from collections import Counter

def remAnagram(str1, str2):
	s1 = Counter(str1)
	s2 = Counter(str2)
	tot = 0
	for i in s1:
		if i not in s2:
			tot += s1[i]
			s1[i] = 0
		elif s2[i] != s1[i]:
			tot += abs(s2[i] - s1[i])
			s2[i] = 0
			s1[i] = 0
	for i in s2:
		if i not in s1:
			tot += s2[i]
		elif s2[i] != s1[i]:
			tot += abs(s2[i] - s1[i])
	return tot


def remAnagram(str1, str2):
	a = [0] * 26
	b = [0] * 26
	for i in range(len(str1)):
		a[ord(str1[i]) % 97] += 1
	for i in range(len(str2)):
		b[ord(str2[i]) % 97] += 1
	c = 0
	for i in range(26):
		c += abs(a[i] - b[i])
	return c


import string

def remAnagram(s1, s2):
	d1 = dict.fromkeys(string.ascii_lowercase, 0)
	d2 = d1.copy()
	for i in s1:
		d1[i] += 1
	for i in s2:
		d2[i] += 1
	c = 0
	for i in d1:
		c += abs(d1[i] - d2[i])
	return c


from collections import Counter

def remAnagram(str1, str2):
	str1 = sorted(str1)
	str2 = sorted(str2)
	first_String = dict(Counter(str1))
	second_String = dict(Counter(str2))
	lst_1 = []
	for (key, value) in first_String.items():
		if key not in second_String.keys():
			lst_1.append(first_String[key])
		if key in second_String.keys():
			if value != second_String[key]:
				lst_1.append(abs(value - second_String[key]))
	for (key, value) in second_String.items():
		if key not in first_String.keys():
			lst_1.append(second_String[key])
	result_1 = sum(lst_1)
	return result_1


def remAnagram(str1, str2):
	s1 = [x for x in str1]
	s2 = [x for x in str2]
	count = 0
	i = 0
	while i <= len(s1) - 1:
		char = s1[i]
		if char not in s2:
			s1.remove(char)
			count += 1
			continue
		s1.remove(char)
		s2.remove(char)
	i = 0
	while i <= len(s2) - 1:
		char = s2[i]
		if char not in s1:
			s2.remove(char)
			count += 1
			continue
		s1.remove(char)
		s2.remove(char)
	return count


def remAnagram(str1, str2):

	def ang(s):
		l = [0] * 26
		for i in s:
			l[ord(i) - 97] += 1
		return l
	c = 0
	f1 = ang(str1)
	f2 = ang(str2)
	for i in range(len(f1)):
		c += abs(f1[i] - f2[i])
	return c


def remAnagram(str1, str2):
	count1 = [0] * 26
	count2 = [0] * 26
	for i in range(len(str1)):
		count1[ord(str1[i]) - ord('a')] += 1
	for i in range(len(str2)):
		count2[ord(str2[i]) - ord('a')] += 1
	result = 0
	for i in range(26):
		result += abs(count1[i] - count2[i])
	return result


def remAnagram(str1, str2):
	c = [0] * 26
	for x in str1:
		c[ord(x) - 97] += 1
	for x in str2:
		c[ord(x) - 97] -= 1
	return sum(map(abs, c))


def remAnagram(str1, str2):
	arr = {}
	for i in str1:
		if i in arr:
			arr[i] += 1
		else:
			arr[i] = 1
	for i in str2:
		if i in arr:
			arr[i] -= 1
		else:
			arr[i] = -1
	out = 0
	for i in arr.values():
		out += abs(i)
	return out


def remAnagram(str1, str2):
	for char in str1:
		if char in str2:
			str1 = str1.replace(char, '', 1)
			str2 = str2.replace(char, '', 1)
	return len(str1 + str2)


def remAnagram(str1, str2):
	x = {}
	count = []
	n = 0
	for i in str1:
		if i in x:
			x[i] += 1
		else:
			x[i] = 1
	for j in str2:
		if j in x:
			x[j] = x[j] - 1
		else:
			n += 1
	for k in x:
		count.append(abs(x[k]))
	z = sum(count)
	return z + n


def remAnagram(str1, str2):
	ord_s1 = [0] * 26
	ord_s2 = [0] * 26
	for i in str1:
		ord_s1[ord(i) - 97] += 1
	for i in str2:
		ord_s2[ord(i) - 97] += 1
	count = 0
	for i in range(26):
		if ord_s1[i] != ord_s2[i]:
			count += abs(ord_s1[i] - ord_s2[i])
	return count


def remAnagram(str1, str2):
	c1 = [0] * 26
	c2 = [0] * 26
	for i in range(0, len(str1)):
		c1[ord(str1[i]) - ord('a')] += 1
	for j in range(0, len(str2)):
		c2[ord(str2[j]) - ord('a')] += 1
	result = 0
	for i in range(0, 26):
		result += abs(c1[i] - c2[i])
	return result


def remAnagram(str1, str2):
	freq1 = {}
	freq2 = {}
	for i in str1:
		if i not in freq1:
			freq1[i] = 0
		freq1[i] += 1
	for i in str2:
		if i not in freq2:
			freq2[i] = 0
		freq2[i] += 1
	c = 0
	for i in freq1:
		if i not in freq2:
			c += freq1[i]
		elif freq1[i] > freq2[i]:
			c += freq1[i] - freq2[i]
	for i in freq2:
		if i not in freq1:
			c += freq2[i]
		elif freq2[i] > freq1[i]:
			c += freq2[i] - freq1[i]
	return c


def remAnagram(str1, str2):
	freq1 = [0] * 26
	freq2 = [0] * 26
	for i in str1:
		freq1[ord(i) - ord('a')] += 1
	for i in str2:
		freq2[ord(i) - ord('a')] += 1
	diff = 0
	for i in range(26):
		diff += abs(freq1[i] - freq2[i])
	return diff


import collections

def remAnagram(str1, str2):
	d1 = [0] * 26
	d2 = [0] * 26
	ans = 0
	for i in range(len(str1)):
		d1[ord(str1[i]) - 97] += 1
	for i in range(len(str2)):
		d2[ord(str2[i]) - 97] += 1
	for i in range(26):
		ans += abs(d1[i] - d2[i])
	return ans


def remAnagram(s1, s2):
	d = {}
	c = 0
	n = 0
	for i in s1:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	for j in s2:
		if j in d and d[j] > 0:
			d[j] = d[j] - 1
		else:
			c += 1
	for k in d:
		c += d[k]
	return c


def remAnagram(str1, str2):
	freq_map = {}
	for char in str1:
		if char in freq_map:
			freq_map[char] += 1
		else:
			freq_map[char] = 1
	for char in str2:
		if char in freq_map:
			freq_map[char] -= 1
		else:
			freq_map[char] = -1
	diff_sum = 0
	for char in freq_map.keys():
		diff_sum += abs(freq_map[char])
	return diff_sum


def remAnagram(str1, str2):
	fre_str1 = [0] * 26
	fre_str2 = [0] * 26
	diff = 0
	for c in str1:
		fre_str1[ord(c) - ord('a')] += 1
	for c in str2:
		fre_str2[ord(c) - ord('a')] += 1
	for i in range(26):
		diff += abs(fre_str1[i] - fre_str2[i])
	return diff


def remAnagram(str1, str2):
	str1_length = len(str1)
	str2_length = len(str2)
	a1 = [0] * 26
	a2 = [0] * 26
	count = 0
	for i in range(0, str1_length):
		a1[ord(str1[i]) - ord('a')] += 1
	for i in range(0, str2_length):
		a2[ord(str2[i]) - ord('a')] += 1
	for i in range(0, 26):
		count = count + abs(a1[i] - a2[i])
	return count


def remAnagram(str1, str2):
	m = {}
	count = 0
	for i in str2:
		if i in m:
			m[i] += 1
		else:
			m[i] = 1
	for i in str1:
		if i in m:
			if m[i] == 1:
				m.pop(i)
				continue
			m[i] -= 1
		else:
			count += 1
	s = 0
	for key in m:
		s += m[key]
	return count + s


def remAnagram(str1, str2):
	for i in range(len(str1)):
		if str1[i] in str2:
			str2 = str2.replace(str1[i], '*', 1)
			str1 = str1.replace(str1[i], '*', 1)
	for i in range(len(str2)):
		if str2[i] in str1:
			str2 = str2.replace(str2[i], '*', 1)
			str1 = str1.replace(str2[i], '*', 1)
	c = len(str1) - str1.count('*') + len(str2) - str2.count('*')
	return c


def remAnagram(str1, str2):
	a = min(str1, str2)
	if a == len(str1):
		st_min = str1
		st_max = str2
	else:
		st_min = str2
		st_max = str1
	for i in range(len(st_min)):
		if st_min[i] in st_max:
			st_max = st_max.replace(st_min[i], '*', 1)
			st_min = st_min.replace(st_min[i], '*', 1)
	for i in range(len(st_max)):
		if st_max[i] in st_min:
			st_max = st_max.replace(st_max[i], '*', 1)
			st_min = st_min.replace(st_max[i], '*', 1)
	c = len(st_max) - st_max.count('*') + len(st_min) - st_min.count('*')
	return c


def remAnagram(s1, s2):
	for i in s2:
		if i in s1:
			s1 = s1.replace(i, '', 1)
			s2 = s2.replace(i, '', 1)
	return len(s1) + len(s2)


def remAnagram(S1, S2):
	freq1 = {}
	freq2 = {}
	for char in S1:
		if char in freq1:
			freq1[char] += 1
		else:
			freq1[char] = 1
	for char in S2:
		if char in freq2:
			freq2[char] += 1
		else:
			freq2[char] = 1
	total_chars_to_delete = 0
	for char in freq1.keys():
		if char in freq2:
			total_chars_to_delete += abs(freq1[char] - freq2[char])
		else:
			total_chars_to_delete += freq1[char]
	for char in freq2.keys():
		if char not in freq1:
			total_chars_to_delete += freq2[char]
	return total_chars_to_delete


def remAnagram(str1, str2):
	count_s1 = dict()
	count_s2 = dict()
	for s in str1:
		count_s1[s] = count_s1.get(s, 0) + 1
	for s in str2:
		count_s2[s] = count_s2.get(s, 0) + 1
	count = 0
	for s in count_s1:
		if s in count_s2:
			count += min(count_s1[s], count_s2[s])
	return len(str1) + len(str2) - 2 * count


def remAnagram(str1, str2):
	S1 = len(str1)
	S2 = len(str2)
	str1 = list(str1)
	str2 = list(str2)
	dup = 0
	if S1 > S2:
		for i in str2:
			if i in str1:
				dup = dup + 1
				str1.remove(i)
	else:
		for i in str1:
			if i in str2:
				dup = dup + 1
				str2.remove(i)
	return S1 - dup + (S2 - dup)


def remAnagram(str1, str2):
	for x in str2:
		if x in str1:
			str1 = str1.replace(x, '', 1)
			str2 = str2.replace(x, '', 1)
	return len(str1 + str2)


def remAnagram(str1, str2):
	count = 0
	dic1 = {}
	dic2 = {}
	for i in str1:
		if i not in dic1:
			dic1[i] = 1
		else:
			dic1[i] += 1
	for i in str2:
		if i not in dic2:
			dic2[i] = 1
		else:
			dic2[i] += 1
	str1 = list(set(list(str1)))
	for i in str1:
		if i not in dic2:
			count += dic1[i]
		else:
			count += abs(dic1[i] - dic2[i])
	str2 = list(set(list(str2)))
	for i in str2:
		if i not in dic1:
			count += dic2[i]
	return count


def remAnagram(str1, str2):
	freq = {}
	for ch in str1:
		freq[ch] = freq.get(ch, 0) + 1
	count = 0
	for ch in str2:
		if ch not in freq:
			count += 1
		else:
			freq[ch] -= 1
			if freq[ch] == 0:
				freq.pop(ch)
	for ch in freq:
		count += freq[ch]
	return count


def remAnagram(str1, str2):
	count = 0
	charDict = {}
	for char in str1:
		if char in charDict:
			charDict[char] += 1
		else:
			charDict[char] = 1
	for char in str2:
		if char in charDict:
			charDict[char] -= 1
		else:
			charDict[char] = -1
	for char in charDict:
		count += abs(charDict[char])
	return count


def remAnagram(str1, str2):
	c = 0
	a = ''
	for i in str1:
		if i in str2:
			str1 = str1.replace(i, '', 1)
			str2 = str2.replace(i, '', 1)
	return len(str1) + len(str2)


from collections import Counter

def remAnagram(s, t):
	d1 = Counter(s)
	d2 = Counter(t)
	c = d1 & d2
	va = 0
	for k in c:
		va = va + c[k]
	return len(s) + len(t) - 2 * va


def remAnagram(str1, str2):
	c = 0
	ab = list(set(str1 + str2))
	for i in ab:
		c += abs(str1.count(i) - str2.count(i))
	return c


def remAnagram(str1, str2):
	str1 = list(str1)
	str2 = list(str2)
	i = 0
	n = len(str1)
	while i < n:
		if str1[i] in str2:
			str2.remove(str1[i])
			str1.pop(i)
			n -= 1
		else:
			i += 1
	return len(str1) + len(str2)


def remAnagram(str1, str2):
	set1 = set(str1)
	set2 = set(str2)
	dic1 = {}
	for i in set1:
		dic1[i] = str1.count(i)
	dic2 = {}
	for i in set2:
		dic2[i] = str2.count(i)
	for i in set1:
		if i in set2:
			if dic1[i] == dic2[i]:
				dic2[i] = 0
			elif dic1[i] > dic2[i]:
				dic2[i] = dic1[i] - dic2[i]
			elif dic1[i] < dic2[i]:
				dic2[i] = dic2[i] - dic1[i]
		else:
			dic2[i] = str1.count(i)
	count = 0
	for i in dic2:
		count += dic2[i]
	return count


def remAnagram(str1, str2):
	list1 = [0] * 26
	list2 = [0] * 26
	for i in str1:
		list1[ord(i) - ord('a')] += 1
	for i in str2:
		list2[ord(i) - ord('a')] += 1
	result = 0
	for i in range(26):
		result += abs(list1[i] - list2[i])
	return result


from collections import Counter

def remAnagram(str1, str2):
	ct = 0
	d1 = Counter(str1)
	d2 = Counter(str2)
	for key_ in d1:
		if key_ not in d2:
			ct += d1[key_]
		else:
			ct += max(0, d1[key_] - d2[key_])
	for key_ in d2:
		if key_ not in d1:
			ct += d2[key_]
		else:
			ct += max(0, d2[key_] - d1[key_])
	return ct


from collections import Counter

def remAnagram(str1, str2):
	d1 = Counter(str1)
	d2 = Counter(str2)
	total = d1 - d2 + (d2 - d1)
	return sum(total.values())


def remAnagram(s1, s2):
	freq = [0] * 26
	for char in s1:
		freq[ord(char) - ord('a')] += 1
	for char in s2:
		freq[ord(char) - ord('a')] -= 1
	num_remove_chars = 0
	for c in freq:
		num_remove_chars += abs(c)
	return num_remove_chars


def remAnagram(s1, s2):
	freq = {}
	for char in s1:
		freq[char] = freq.get(char, 0) + 1
	for char in s2:
		freq[char] = freq.get(char, 0) - 1
	sum_ = 0
	for i in freq.values():
		sum_ += abs(i)
	return sum_