Description

Often times, just knowing if a number exists in an array is not enough. You want to know how much exists at a given point.
?
This can be done using a counter

Reach for when

Runtime

O(n)

Visualization

Counters-1783975122026.webp432

Pseudocode

counts = {}
for item in items:
	counts[item]+=1

for item in items:
	if case(item, counts):
		update counts
		if counts not valid
			break

return answer

Code

def is_anagram(s, t):
	counts = {}
	for item in s:
		counts[item] = counts.get(item, 0) + 1

	for item in t:
		counts[item] = counts.get(item, 0) - 1
		if counts[item] < 0:  # counts not valid
			return False

	return True