博客
关于我
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中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
MySQL中DQL语言的执行顺序
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中kill掉所有锁表的进程
查看>>
mysql中like % %模糊查询
查看>>
MySql中mvcc学习记录
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>