Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I have this idea (using C language) for checking for two string if they are anagrams-

  1. if the length of the strings is the same (its only a-z and A-Z).

  2. sum of ASCII value of all chars in a string is the same for both strings.

  3. multiplication of ASCII value of all chars in a string is the same for both strings.

I believe that if all three are correct it must be an anagram but I cannot prove it.. since I have no math background.. can any one prove it or contradict it?

(other solutions for this problem are not necessery)

share|improve this question

2 Answers 2

A minimal counterexample is "BPT" (ASCII $66$, $80$, $84$) and "FHX" (ASCII $70$, $72$, $88$). They're the same length, the sums are both $230$, and the products are both $443520$. As mentioned in the other answer, you will probably need all $n$ symmetric functions of the ASCII codes to correctly identify all non-anagrammatic pairs of length $n$. For instance, for $n=3$, in addition to $x_1+x_2+x_3$ and $x_1 x_2 x_3$, you can include $x_1 x_2 + x_1 x_3 + x_2 x_3$ in your "key".

share|improve this answer
    
The first counterexample in lex order is AHM and BFN. –  lhf Feb 6 '13 at 23:42

If the string has length $n$, you most probably need all $n$ symmetric functions of the chars, just the two you mentioned won't do.

Try to find an example with $n=3$.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.