当前位置:网站首页>代码随想录笔记_哈希_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;
}
边栏推荐
猜你喜欢

mysql8.0.28下载和安装详细教程,适配win11

JSP Webshell free kill

MySQL8--Windows下使用压缩包安装的方法

面试必备!TCP协议经典十五连问!

MySQL8 -- use msi (graphical user interface) under Windows installation method

MySQL8.0.28 installation tutorial

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

AntV X6制作画板工具(图形,线段,图片上传)

2W字!详解20道Redis经典面试题!(珍藏版)

ModuleNotFoundError: No module named ‘openpyxl‘
随机推荐
线性代数学习笔记3-3:逆矩阵的理解
深度自编码网络的集成学习ICPS入侵检测模型
MySQL中的各种锁(行锁、间隙锁、临键锁等等LBCC)
MongoDB文档存储
I will give you a chance to interview in a big factory. Can you interview?Come in and see!
IPIDEA的使用方式
rem adaptation
为什么四个字节的float表示的范围比八个字节的long要广
mysql8.0.28下载和安装详细教程,适配win11
JSP Webshell 免杀
STM32——LCD—TFTLCD原理与配置介绍
MySQL中的时间函数
Heao Technology Network Interview (with reference answers)
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)
JSP Webshell free kill
聊聊flink的BoundedOutOfOrdernessTimestampExtractor
暴力破解全攻略
centos安装mysql8
LeetCode:1161. 最大层内元素和【BFS层序遍历】
Keil development environment installation tutorial