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

你可能感兴趣的文章
Node.js安装和入门 - 2行代码让你能够启动一个Server
查看>>
node.js安装方法
查看>>
Node.js官网无法正常访问时安装NodeJS的方法
查看>>
node.js模块、包
查看>>
node.js模拟qq漂流瓶
查看>>
node.js的express框架用法(一)
查看>>
Node.js的交互式解释器(REPL)
查看>>
Node.js的循环与异步问题
查看>>
Node.js高级编程:用Javascript构建可伸缩应用(1)1.1 介绍和安装-安装Node
查看>>
nodejs + socket.io 同时使用http 和 https
查看>>
NodeJS @kubernetes/client-node连接到kubernetes集群的方法
查看>>
NodeJS API简介
查看>>
nodejs Error: request entity too large解决方案
查看>>
Nodejs express 获取url参数,post参数的三种方式
查看>>
nodejs http小爬虫
查看>>