Gem problem

J given string represents the type of gem stones, and S represents a string of stone you have. S Each character represents a type of stone you have, you want to know the stone you have in how many gems.
J letters will not be repeated, J S and all the characters are letters. A case-sensitive, so "a" and "A" are different types of stones.

Example 1:
Input J = "aA", S = "aAAbbbb"
Output: 3

Example 2:
Input J = "z", S = "ZZ"
Output: 0

note:

J S and containing up to 50 letters.
J The characters are not repeated.

let a = (J, S) => {
    let bingo = 0;
    for (let i = 0; i < J.length; i++) {
        let reg = new RegExp(`${J[i]}`, `g`);
        //get!匹配不确定的变量类型可以用构造函数模式的模板字符串添加进去
        //直接引号以字符串添加也行
        //reg.global/ignoreCase=true  是gi的另一种表示。
        let res;
        while (res = reg.exec(`${S}}`)) {
            //实测match括号内不能用模板字符串
            bingo++;
        }
    }
    return bingo;
}
let J_Str = "aB";
let S_Str = "aaAbbBBbokkefje";
console.log(a(J_Str, S_Str));//4

Guess you like

Origin www.cnblogs.com/Syinho/p/12633986.html