Two strings are anagrams of each other if and only if the sum and product of the characters of the strings are same. How?

StackOverflow https://stackoverflow.com/questions/18768757

  •  28-06-2022
  •  | 
  •  

Frage

I was reading an algorithmic problem at http://learn.hackerearth.com/question/314/finding-non-anagramic-strings-in-a-list/

I came across the following claim:

Two strings (of same size) are anagrams of each other if and only if the sum and product of the characters of the two strings are same (treat A => 1, B => 2, ..., Z => 26).

I tried to prove this but I failed. Can someone prove this claim?

War es hilfreich?

Lösung

The claim is really false. Following is a counter example.

  • ABBI: Sum = 14, Product = 36
  • AAFF: Sum = 14, Product = 36

Andere Tipps

As shown in the answer above this logic does not holds good. I would suggest to check if:

  1. Length of string 1=String 2
  2. if condition 1 is true then Sort String 1 and String 2 and compare them
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top