How can I compare two shuffled strings?

后端 未结 5 972
盖世英雄少女心
盖世英雄少女心 2021-02-02 08:05

I have the following two strings:

var str1 = \"hello\";
var str2 = \"ehlol\";

How can I check whether both strings contain the same characters?

5条回答
  •  醉酒成梦
    2021-02-02 08:36

    You could possibly say this:

    (a.length === b.length) &&   (a.split('').every(function(val) { return b.indexOf(val) > -1}))
    

    And, in ES6 you could make it look as follows:

    (a.length === b.length) && a.split('').every(val => { return b.indexOf(val) > -1 })
    

提交回复
热议问题