当前位置:网站首页>leetcode刷题:哈希表06 (赎金信)
leetcode刷题:哈希表06 (赎金信)
2022-06-23 17:23:00 【涛涛英语学不进去】
383. 赎金信
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)
/** * 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 * 如果可以,返回 true ;否则返回 false 。 * magazine 中的每个字符只能在 ransomNote 中使用一次。 * <p> * 这个题不就是问两个是不是字母异位词嘛。 * * @param ransomNote * @param magazine * @return */
public static boolean canConstruct(String ransomNote, String magazine) {
final byte[] magazineBytes = magazine.getBytes();
HashMap<Byte, Integer> map = new HashMap<>();
for (byte i : magazineBytes) {
//如果不包含,添加,包含则数量+1
if (map.containsKey(i)) {
map.put(i, map.get(i) + 1);
} else {
map.put(i, 1);
}
}
final byte[] ransomNoteBytes = ransomNote.getBytes();
int value;
for (byte i : ransomNoteBytes) {
//如果不包含,则false
if (!map.containsKey(i)) {
return false;
} else {
//如果这个字母只剩最后一次使用机会了,则删除这个键
value = map.get(i);
if (value == 1) {
map.remove(i);
} else {
map.put(i, value - 1);
}
if (map.size() == 0) {
return true;
}
}
}
return true;
}

很像字母异位同那题,但不全是,只要右边完全包含左边即可,需要计数。
终极思路:使用数组做,看起来也简洁很多,虽然思路一样的。
/** * 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 * 如果可以,返回 true ;否则返回 false 。 * magazine 中的每个字符只能在 ransomNote 中使用一次。 * <p> * 这个题不就是问两个是不是字母异位词嘛。 * * @param ransomNote * @param magazine * @return */
public static boolean canConstruct(String ransomNote, String magazine) {
final byte[] magazineBytes = magazine.getBytes();
int[] result = new int[26];
for (byte i : magazineBytes) {
result[i - 'a'] += 1;
}
final byte[] ransomNoteBytes = ransomNote.getBytes();
for (byte i : ransomNoteBytes) {
if (result[i - 'a'] <= 0) {
return false;
}else {
result[i - 'a'] -= 1;
}
}
return true;
}

边栏推荐
- 《致敬百年巨匠 , 数藏袖珍书票》
- An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
- Paper reading (55):dynamic multi robot task allocation under uncertainty and temporary constraints
- 一元二次方程到规范场
- 微信小程序报错[ app.json 文件内容错误] app.json: app.json 未找到
- Script to view the execution of SQLSERVER database stored procedures
- 视频异常检测数据集 (ShanghaiTech)
- Wechat applet reports an error [app.json file content error] app json: app. JSON not found
- 12. Manage network environment
- 【Unity】插件TextAnimator 新手使用说明
猜你喜欢

论文阅读 (53):Universal Adversarial Perturbations

《致敬百年巨匠 , 数藏袖珍书票》
![[esp8266-01s] get weather, city, Beijing time](/img/8f/89e6f0d482f482ed462f1ebd53616d.png)
[esp8266-01s] get weather, city, Beijing time
![[win10 vs2019 opencv4.6 configuration reference]](/img/51/62fb26123561b65f127304ede834a2.png)
[win10 vs2019 opencv4.6 configuration reference]

Imeta | Nannong shenqirong team released microbial network analysis and visualization R package ggclusternet

VNC Viewer方式的远程连接树莓派

3000帧动画图解MySQL为什么需要binlog、redo log和undo log

深入理解 padding

Paper reading (56):muti features predction of protein translational modification sites (task)

esp8266-01s 不能连接华为路由器解决方法
随机推荐
[esp8266-01s] get weather, city, Beijing time
Asynchronous or thread pool
芯片原厂必学技术之理论篇(4-1)时钟技术、复位技术
论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
对抗攻击与防御 (2):对抗样本的反制策略
Baidu AI Cloud product upgrade Observatory in May
csdn涨薪秘籍之Jenkins集成allure测试报告全套教程
12. Manage network environment
Implementing Domain Driven Design - using ABP framework - General guidelines
7、VLAN-Trunk
论文阅读 (57):2-hydr_Ensemble: Lysine 2-Hydroxyisobutyrylation Identification with Ensemble Method (任务)
TT 语音落地 Zadig:开源共创 Helm 接入场景,环境治理搞得定!
2021 excellent TDP members' output data summary is coming, come and watch!!!
esp8266-01s 不能连接华为路由器解决方法
[Hyperf]Entry “xxxInterface“ cannot be resolved: the class is not instantiable
How to make a badge
How do I write a small program that can automatically edit new year greetings
论文阅读 (49):Big Data Security and Privacy Protection (科普文)
QML类型:Loader
《致敬百年巨匠 , 数藏袖珍书票》