博客
关于我
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/

你可能感兴趣的文章
MySQL通用优化手册
查看>>
Mysql通过data文件恢复
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
mysql部署错误
查看>>
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
mysql重新安装?忘记root密码?重装Windows、Linux系统导致mysql没法用吗? 这里有你想要的答案
查看>>
mysql重置root密码
查看>>
MySQL锁
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>