• #力扣:1684. 统计一致字符串的数目@FDDLC


    1684. 统计一致字符串的数目 - 力扣(LeetCode)

    一、Java

    1. class Solution {
    2. public int countConsistentStrings(String allowed, String[] words) {
    3. boolean[] isAllowed = new boolean[26];
    4. for(int i = 0; i < allowed.length(); i++) isAllowed[allowed.charAt(i)-'a'] = true;
    5. int cnt = 0;
    6. for(String s: words) {
    7. boolean flag = true;
    8. for(int i = s.length() - 1; i >= 0; i--){
    9. if(!isAllowed[s.charAt(i) - 'a']){
    10. flag = false;
    11. break;
    12. }
    13. }
    14. if(flag)cnt++;
    15. }
    16. return cnt;
    17. }
    18. }

    二、C++

    1. #include
    2. #include
    3. using namespace std;
    4. class Solution {
    5. public:
    6. int countConsistentStrings(string allowed, vector &words) {
    7. bool isAllowed[26] = {};
    8. for (char c: allowed) isAllowed[c - 'a'] = true;
    9. int cnt = 0;
    10. for (string s: words) {
    11. bool flag = true;
    12. for (char c: s) {
    13. if (!isAllowed[c - 'a']) {
    14. flag = false;
    15. break;
    16. }
    17. }
    18. if(flag) cnt++;
    19. }
    20. return cnt;
    21. }
    22. };

    三、Python

    1. from typing import List
    2. class Solution:
    3. def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
    4. allowedSet = set(allowed)
    5. cnt = 0
    6. for s in words:
    7. flag = True
    8. for c in s:
    9. if c not in allowedSet:
    10. flag = False
    11. break
    12. if flag:
    13. cnt += 1
    14. return cnt

    四、JavaScript

    1. var countConsistentStrings = function (allowed, words) {
    2. allowedSet = new Set(allowed);
    3. let cnt = 0;
    4. for (let s of words) {
    5. let flag = true;
    6. for (let c of s) {
    7. if (!allowedSet.has(c)) {
    8. flag = false;
    9. break;
    10. }
    11. }
    12. if (flag) cnt++;
    13. }
    14. return cnt;
    15. };

    五、Go

    1. package main
    2. func countConsistentStrings(allowed string, words []string) int {
    3. var isAllowed [26]bool
    4. for _, c := range allowed {
    5. isAllowed[c-'a'] = true
    6. }
    7. cnt := 0
    8. for _, s := range words {
    9. flag := true
    10. for _, c := range s {
    11. if !isAllowed[c-'a'] {
    12. flag = false
    13. break
    14. }
    15. }
    16. if flag {
    17. cnt++
    18. }
    19. }
    20. return cnt
    21. }

  • 相关阅读:
    centost7下安装oracle11g 总结踩坑
    Mysql索引特性(重要)
    51单片机 矩阵键盘
    数据结构-双向链表操作
    介绍一个中后台管理系统
    Redis--线程模型详解
    C++匿名函数lambda详解
    零基础入门学习Python第二阶04SQL详解03
    论文阅读笔记 | 三维目标检测——3DSSD
    DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基MW:1000
  • 原文地址:https://blog.csdn.net/liuxc324/article/details/133935608