博客
关于我
leetcode之统计一致字符串的数目(C++)
阅读量:160 次
发布时间:2019-02-28

本文共 927 字,大约阅读时间需要 3 分钟。

参考链接

  1. https://leetcode-cn.com/problems/count-the-number-of-consistent-strings

题目描述

给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是一致字符串。

请你返回words数组中一致字符串的数目。

在这里插入图片描述

解题思路

用哈希集合存储allowed字符串中的字母,然后逐个遍历words中的字符串是否有不存在与集合中的字母。

代码

class Solution {   public:    int countConsistentStrings(string allowed, vector
& words) { unordered_set
allowed_chars; int res = 0; for (int i = 0; i < allowed.size(); i ++) { allowed_chars.insert(allowed[i]); } for (int i = 0; i < words.size(); i ++) { int j = 0; for (; j < words[i].size(); j ++) { if (allowed_chars.count(words[i][j]) == 0) { break; } } if (j == words[i].size()) { res ++; } } return res; }};

转载地址:http://ulxj.baihongyu.com/

你可能感兴趣的文章
NFS Server及Client配置与挂载详解
查看>>
NFS 服务配置篇
查看>>
NFS共享文件系统搭建
查看>>
nfs复习
查看>>
NFS安装配置
查看>>
NFS服务器配置-服务启动与停止
查看>>
NFS的安装以及windows/linux挂载linux网络文件系统NFS
查看>>
NFS的常用挂载参数
查看>>
NFS网络文件系统
查看>>
NFS远程目录挂载
查看>>
nft文件传输_利用remoting实现文件传输-.NET教程,远程及网络应用
查看>>
NFV商用可行新华三vBRAS方案实践验证
查看>>
ng build --aot --prod生成文件报错
查看>>
ng 指令的自定义、使用
查看>>
ng6.1 新特性:滚回到之前的位置
查看>>
nghttp3使用指南
查看>>
Nginx
查看>>
nginx + etcd 动态负载均衡实践(一)—— 组件介绍
查看>>
nginx + etcd 动态负载均衡实践(三)—— 基于nginx-upsync-module实现
查看>>
nginx + etcd 动态负载均衡实践(二)—— 组件安装
查看>>