当前位置:网站首页>Ransom Letter Questions and Answers
Ransom Letter Questions and Answers
2022-08-10 19:57:00 【little question mark we are friends】
Given the stem:
Give you two strings: ransomNote and magazine , judge whether ransomNote can be composed of the characters in magazine.
Returns true if yes; false otherwise.
Each character in the magazine can only be used once in ransomNote.
Title source:
LeetCode
1. Example
The code is as follows (example):
Input: ransomNote = "aa", magazine = "ab"output: falseInput: ransomNote = "aa", magazine = "aab"output: true
2. Answers and NotesRemember
The code is as follows (Java):
class Solution {public boolean canConstruct(String ransomNote, String magazine) {int rLen = ransomNote.length();int mLen = magazine.length();char[] rChr = ransomNote.toCharArray();char[] mChr = magazine.toCharArray();// If the length of the ransomNote is greater than the length of the magazine, the ransomNote cannot be composed of the characters in the magazine.if(rLen > mLen) {return false;}// Create a count array to store the number of times each letter appears.The index of the array is 0-25, and the index value can be obtained by the difference of any two of the values 97-122 of a-z in the ASCII table.int[] cInt = new int[26];// Loop through the two character arrays respectively. When the letter appears in the magazine, the value of the count array is increased by one, and when the letter appears in the ransomNote, the value of the count array is reduced by one.for(char c:mChr) {// c - 'a' means to get the difference between the ASCII code of the current character and the ASCII code of the character 'a', which is the index of the character in the count array.cInt[c - 'a']++;}for(char c:rChr) {cInt[c - 'a']--;//If cInt[c - 'a'] is less than zero, then ransomNote cannot be composed of characters in magazine.if(cInt[c - 'a'] < 0) {return false;}}return true;}}
3.ASCII table
Summary
That's all for today. This article introduces the solution to the ransom note problem. Here is a memo for reference.
边栏推荐
- 赎金信问题答记
- [Go WebSocket] 你的第一个Go WebSocket服务: echo server
- Keras deep learning combat (17) - image segmentation using U-Net architecture
- 30分钟使用百度EasyDL实现健康码/行程码智能识别
- YOLOv3 SPP源码分析
- 【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
- When selecting a data destination when creating an offline synchronization node - an error is reported in the table, the database type is adb pg, what should I do?
- 测试/开发程序员值这么多钱么?“我“不会愿赌服输......
- 皮质-皮质网络的多尺度交流
- 怎么完全卸载赛门铁克_Symantec卸载方法,赛门铁克卸载「建议收藏」
猜你喜欢
WCF and TCP message communication practice, c # 】 【 realize group chat function
【知识分享】在音视频开发领域中SEI到底是个啥?
QoS服务质量七交换机拥塞管理
【C#】WCF和TCP消息通信练习,实现群聊功能
[email protected])纳米酶"/>
血红素-金纳米颗粒(Heme-AuNP)复合纳米酶|金纳米颗粒核多孔空心碳纳米球壳([email protected])纳米酶
[email protected]纳米模拟酶|PtCo合金纳米粒子"/>
水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子
Introduction to 3 d games beginners essential 】 【 modeling knowledge
ARouter使用自定义注解处理器,自动生成跳转Activity的代码,避免手动填写和管理path
机器学习|模型评估——AUC
We used 48h to co-create a web game: Dice Crush, to participate in international competitions
随机推荐
flask装饰器版登录、session
子域名收集&Google搜索引擎语法
idea汉化教程[通俗易懂]
【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习
优化是一种习惯●出发点是'站在靠近临界'的地方
(12) findContours function hierarchy explanation
Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】
QoS Quality of Service Seven Switch Congestion Management
转铁蛋白修饰蛇床子素长循环脂质体/负载三七皂苷R1的PEG-PLGA纳米粒([email protected] NPs)
servlet映射路径匹配解析
(十二) findContours函数的hierarchy详解
Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
GBASE 8s 高可用RSS集群搭建
力扣150-逆波兰表达式求值——栈实现
Optimizing Bloom Filter: Challenges, Solutions, and Comparisons论文总结
【luogu CF1534F2】Falling Sand (Hard Version)(性质)(dfs)(线段树 / 单调队列 / 贪心)
铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶
巧用RoaringBitMap处理海量数据内存diff问题
水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子
Common ports and services