博客
关于我
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 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>