当前位置:网站首页>代码随想录笔记_哈希_383赎金信
代码随想录笔记_哈希_383赎金信
2022-08-02 03:14:00 【Erik_Won】
代码随想录笔记_哈希表
代码随想录二刷笔记记录
LC 383.赎金信
题目
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1:
输入:ransomNote = “a”, magazine = “b”
输出:false
示例 2:
输入:ransomNote = “aa”, magazine = “ab”
输出:false
示例 3:
输入:ransomNote = “aa”, magazine = “aab”
输出:true
思路分析
step1: 声明一个长度为26的字典,记录magazine里字母出现的次数
step2: 用ransomNote去验证这个字典是否包含了ransomNote中的所有字母。
代码实现
完整代码实现
public boolean canConstruct(String ransomNote, String magazine) {
int[] cnt = new int[26];
char[] ca1 = ransomNote.toCharArray();
for (char c : ca1) {
cnt[c - 'a']++;
}
char[] ca2 = magazine.toCharArray();
for (char c : ca2) {
cnt[c-'a']--;
}
for (int i : cnt) {
if (i > 0){
return false;
}
}
return true;
}
边栏推荐
猜你喜欢
Redis主从、哨兵、 Cluster集群一锅端!
JDBC的入门使用
Scaffolding installation
Hit the programmer interview scene: What did Baidu interviewers ask me?
ASP WebShell backdoor script and anti-kill
Daily practice------There are n integers, so that the previous numbers are moved back m positions in order, and the last m numbers become the first m numbers
(forwarded) HashCode summary (2)
bgp机房的动态路由和静态路由的区别
LeetCode:1161. 最大层内元素和【BFS层序遍历】
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)
随机推荐
Day34 LeetCode
【遥控器开发基础教程3】疯壳·开源编队无人机-ADC(摇杆控制)
7-40 奥运排行榜 (25 分)多项排序
“带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)
MySQL8--Windows下使用msi(图形界面)安装的方法
LeetCode:1161. 最大层内元素和【BFS层序遍历】
精益思想如何加速企业的全局价值流动?
基于优化的多核局部费舍尔判别分析的故障分类
CV-Model【4】:MobileNet v3
深度自编码网络的集成学习ICPS入侵检测模型
WebShell connection tools (Chinese kitchen knife, WeBaCoo, Weevely) use
STL入门基础 map和set容器
基于时延估计的动力型下肢假肢分段控制策略研究
WebShell Feature Value Summary and Detection Tool
Flexbox flex property
线性代数学习笔记3-3:逆矩阵的理解
2022ACM夏季集训周报(五)
多个el-select下拉框无法选中相同内容
5.nodejs--cross domain, CORS, JSONP, Proxy