5

[IT 邦帮忙] 优化或重写方法,使计算长字符串时尽可能缩短运算时间

 3 years ago
source link: https://www.v2ex.com/t/788339
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  JavaScript

[IT 邦帮忙] 优化或重写方法,使计算长字符串时尽可能缩短运算时间

  ciddechan · 12 小时 42 分钟前 · 317 次点击

如果 str1 包含 str2 的所有字母(包括数量),为真,反之为假

function scramble(str1, str2) {
let s1 = str1.split("").sort()
let s2 = str2.split("").sort()
let n = 0
s2.forEach(e=>{
let index = s1.indexOf(e)
if(index>-1){
n++
s1.splice(index,1)
}
})

if(n==s2.length && n!=0){
return true
}else{
return false
}
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK