当前位置:网站首页>Leetcode: hash table 06 (ransom letter)
Leetcode: hash table 06 (ransom letter)
2022-06-23 18:26:00 【Taotao can't learn English】
383. Ransom letter
Give a ransom (ransom) String and a magazine (magazine) character string , Judge the first string ransom Can I have a second string magazines The characters inside make up . If it can be constructed , return true ; Otherwise return to false.
( Title Description : In order not to reveal the handwriting of the ransom letter , To search magazines for the letters you need , Make up words to express meaning . Each character in the magazine string can only be used once in the ransom letter string .)
/** * Here are two strings :ransomNote and magazine , Judge ransomNote Can it be done by magazine The characters inside make up . * If possible , return true ; Otherwise return to false . * magazine Each character in can only be in ransomNote Used once in . * <p> * This question is to ask whether two words are heteronymous letters . * * @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) {
// If not , add to , Include quantity +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) {
// If not , be false
if (!map.containsKey(i)) {
return false;
} else {
// If there is only one last chance to use this letter , Then delete this key
value = map.get(i);
if (value == 1) {
map.remove(i);
} else {
map.put(i, value - 1);
}
if (map.size() == 0) {
return true;
}
}
}
return true;
}

It's very similar to the same question , But not all , As long as the right contains the left completely , Need to count .
Ultimate idea : Use arrays to do , It looks a lot simpler , Although the idea is the same .
/** * Here are two strings :ransomNote and magazine , Judge ransomNote Can it be done by magazine The characters inside make up . * If possible , return true ; Otherwise return to false . * magazine Each character in can only be in ransomNote Used once in . * <p> * This question is to ask whether two words are heteronymous letters . * * @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;
}

边栏推荐
- 实现领域驱动设计 - 使用ABP框架 - 存储库
- esp8266-01s 不能连接华为路由器解决方法
- Thesis reading (57):2-hydr_ Ensemble: lysine 2-hydroxyisobutyrylation identification with ensemble method (task)
- Rancher2.6全新Monitoring快速入门
- An error is reported when latex uses \usepackage{hyperref}: paragraph ended before [email protected] @link was complete
- Sword finger offer II 092 Flip character / Sword finger offer II 093 Longest Fibonacci sequence
- README
- Set up your own website (13)
- VNC Viewer方式的远程连接树莓派
- STM32(九)------- CAN
猜你喜欢

第十三届蓝桥杯单片机国赛真题

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):...

Deep understanding of padding

Counter attack and defense (1): counter sample generation in image domain

『忘了再学』Shell流程控制 — 39、特殊流程控制语句

《致敬百年巨匠 , 数藏袖珍书票》

How to solve the problem that the esp8266-01s cannot connect to Huawei routers

org. apache. ibatis. binding. BindingException: Invalid bound statement (not found):...

Redis Cluster

论文阅读 (48):A Library of Optimization Algorithms for Organizational Design
随机推荐
Establishment and use of SSL VPN (OpenVPN)
Wechat applet startlocationupdatebackground() simply realizes rider distribution location
Simpledateformat has thread safety problems in multi-threaded environments.
全局组织结构控制之抢滩登陆
Cryptography involved in IOT device end
Paper reading (50):a novel matrix game with payoffs of maximal belt structure
[sword finger offer] 45 Arrange the array into the smallest number
Latex使用\usepackage{hyperref}报错:paragraph ended before [email protected]@link was complete
【win10 VS2019 opencv4.6 配置参考】
leetcode刷题:哈希表03 (快乐数)
对抗攻击与防御 (2):对抗样本的反制策略
The battlefield of live broadcast e-commerce is not in the live broadcast room
uniapp项目中防止用户重复提交
深入理解 padding
论文阅读 (47):DTFD-MIL: Double-Tier Feature Distillation Multiple Instance Learning for Histopathology..
信用卡产品开发周期从23周缩短至9周,银行运维组织如何转向敏捷?
论文阅读 (51):Integration of a Holonic Organizational Control Architecture and Multiobjective...
视频异常检测数据集 (ShanghaiTech)
Strong cache and negotiation cache in http
The draganddrop framework, a new member of jetpack, greatly simplifies the development of drag and drop gestures!