字符串JavaScript的通配符匹配
我们需要编写一个JavaScript函数,该函数接受两个字符串和一个数字n。该函数匹配两个字符串,即,它检查两个字符串是否包含相同的字符。如果两个字符串都包含相同的字符而不管它们的顺序如何,或者如果它们最多包含n个不同的字符,则函数应该返回true;否则,函数应该返回false。
让我们为该函数编写代码-
示例
const str1 = 'first string';
const str2 = 'second string';
const wildcardMatching = (first, second, num) => {
let count = 0;
for(let i = 0; i < first.length; i++){
if(!second.includes(first[i])){
count++;
};
if(count > num){
return false;
};
};
return true;
};
console.log(wildcardMatching(str1, str2, 2));
console.log(wildcardMatching(str1, str2, 1));
console.log(wildcardMatching(str1, str2, 0));输出结果
控制台中的输出将为-
true true false
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短