Is there any two non equal sets of numbers whose products and sum are equal and have the same size?

65 Views Asked by At

Context: I am doing a computer science project to find the anagram of two array of characters. My algorithm tests if the product and sum of the numbers that represents the characters in the two arrays are equal, and I am thinking if this is mathematically correct.

3

There are 3 best solutions below

0
On BEST ANSWER

If you're using the ASCII values for lowercase letters, then e.g. "bln" and "cip" have the same sum and product: $$ 98+108+110 = 99+105+112 = 316,\ 98 \times 108 \times 110 = 99 \times 105 \times 112 = 1164240$$ Or with upper case, "AHM" and "BFN" is an example.

0
On

If you want to go in that direction, you should rather assign to each character a prime number (that is, assign $2$ to $a$, $3$ to $b$, $5$ to $c$, and so on), and take the products of these numbers.

That being said, there are much more efficient algorithms. For instance, if you have 26 charaters, you could take an array T of size $2$6 (initialized at $0$). Then simply increment T['c' -26] when you find the letter $c$ in the first array of characters, and decrement it when you find $c$ in the second array. Then you have an anagram iff T is an array of $0$'s at the end.

0
On

It is not, as @Robert Israel proved by example.

A classic solution to this problem is checking wether both strings have the same characters in the same quantity. In Python:

from collections import Counter
Counter(a) == Counter(b)