如何比较两个洗牌的字符串? [英] How can I compare two shuffled strings?
本文介绍了如何比较两个洗牌的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下两个字符串:
var str1 = "hello";
var str2 = "ehlol";
如何检查两个字符串是否包含相同的字符?
How can I check whether both strings contain the same characters?
推荐答案
可能不是最优,但你可以做到这一点
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
这篇关于如何比较两个洗牌的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文