zoukankan      html  css  js  c++  java
  • js 统计一致字符串的数目

    给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。
    请你返回 words 数组中 一致字符串 的数目。
    示例 1:

    输入:allowed = “ab”, words = [“ad”,“bd”,“aaab”,“baa”,“badab”]
    输出:2
    解释:字符串 “aaab” 和 “baa” 都是一致字符串,因为它们只包含字符 ‘a’ 和 ‘b’ 。

    /**
     * @param {string} allowed
     * @param {string[]} words
     * @return {number}
     */
    var countConsistentStrings = function(allowed, words) {
        let res=0
        for(let i =0;i<words.length;i++){
            let count = 0
            for(const char of words[i]){
                if(allowed.indexOf(char)>-1){
                    count++
                }else{
                    count=0
                    break
                }
            }
            count? res++:res
        }
        return res
    };
    
    请用今天的努力,让明天没有遗憾。
  • 相关阅读:
    Codeforces Round #218 (Div. 2) C题
    Codeforces Round #217 (Div. 2) c题
    求组合数
    hdu 4539(状态压缩dp)
    poj 2923(状态压缩dp)
    项目管理
    java 实现单向链表
    sql
    设计模式
    自己写了一个解析json为table的工具类
  • 原文地址:https://www.cnblogs.com/cupid10/p/15617565.html
Copyright © 2011-2022 走看看