Compare two frequency maps06.05
Two bags are equal iff their frequency maps are equal: same keys, same counts. Lengths differing already forbids equality. Treat absent as 0. Do not require both maps to store zeros.
You can build both maps, or increment one array and decrement the other (need all zeros / empty at the end).
Same skill, three traces. Read all three before you try it.
01same bags - given
a = [1, 4, 1],b = [4, 1, 1]- walk
{1:2, 4:1}both- result
- true
02different counts - given
a = [1, 1, 4],b = [1, 4, 4]- walk
1:2vs1:1- result
- false
03extra key - given
a = [1, 4],b = [1, 4, 9]- walk
bhas9- result
- false
sameBag(a, b).a and
b are arrays of integers.Return true if their frequency maps match: same keys, same counts.
a = [1, 4, 1], b = [4, 1, 1] → true.a = [1, 1, 4], b = [1, 4, 4] → false.a = [1, 4], b = [1, 4, 9] → false.constraintsa and b are non-empty lists of integers.
- Valid Anagram0/2 skills ready · hash maps · easyopen
- Valid Anagram0/2 skills ready · hash maps · easyopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.