How can you tell that a String is an anagram of another String? - Cracking the Java Coding Interview

preview_player
Показать описание
Cracking the #Java #Coding #Interview - Question 33: How can you tell that a String is an anagram of another String?
Рекомендации по теме
Комментарии
Автор

The prime thing is clever but terrible advice in practice because it's only guaranteed to work for Strings with just 9 characters or less (if you use 64-bit longs). Beyond that length you'd have to use BigIntegers, but then multiplication is no longer an O(1) operation.
Since the sorted char array comparison is much easier to reason about and isn't obviously slower than the alternative, it should be the preferred solution (i.e. avoid premature optimization).

jay_
Автор

I would use a Map, key is a letter, value is the amount of occurrences of the letter. First word is +, the second word is -. If it is the last one then we remove the key completely.

hazartilirot
Автор

second approch won't work if we have something from outsidse of charset

apptrendz
Автор

I'd say it's a bad solution as it has too many caveats. You'd have to make sure that you don't overflow during the multiplication. You need to have the letter-to-prime mapping ready (or calculate primes as part of your algorithm, but then the complexity would be different). If you need O(n) time complexity, don't overcomplicate, just count the occurrences of each letter using an array or a hash map, and then compare the results.

Javakka
Автор

Also these problems are next to meaningless on modern computing for the domain they actually solve

stcattc
Автор

I think is the same...

final String s1 = "CATA";
final String s2 = "TACA";

System.out.println(


)
);

joseramonmas.informatica
welcome to shbcf.ru