How can I compare two shuffled strings?

May not be very optimal, but you can simply do

str1.split("").sort().join() == str2.split("").sort().join(); //outputs true

Another suggested approach in one the comments (for optimization in case string length is quite big)

str1.length===str2.length && str1.split("").sort().join() == str2.split("").sort().join(); //first check the length to quickly rule out in case of obvious non-matches

Leave a Comment