当前位置:网站首页>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.
边栏推荐
- 杭电多校七 1003-Counting Stickmen(组合数学)
- [SemiDrive source code analysis] [MailBox inter-core communication] 52 - DCF Notify implementation principle analysis and code combat
- “2022零信任神兽方阵”启动调研,欢迎各单位填报信息
- Metasploit——渗透攻击模块(Exploit)
- 3D游戏建模学习路线
- Rider调试ASP.NET Core时报thread not gc-safe的解决方法
- 铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
- 基于TCP的聊天系统
- @Autowired注解 --required a single bean, but 2 were found出现的原因以及解决方法
- Linux服务器安装Redis,详细步骤。
猜你喜欢
基于TCP的聊天系统
转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
[教你做小游戏] 只用几行原生JS,写一个函数,播放音效、播放BGM、切换BGM
[Go WebSocket] 你的第一个Go WebSocket服务: echo server
servlet映射路径匹配解析
力扣18-四数之和——双指针法
Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
我们用48h,合作创造了一款Web游戏:Dice Crush,参加国际赛事
从 Delta 2.0 开始聊聊我们需要怎样的数据湖
Tf铁蛋白颗粒包载顺铂/奥沙利铂/阿霉素/甲氨蝶呤MTX/紫杉醇PTX等药物
随机推荐
【CNN】刷SOTA的trick
【Knowledge Sharing】What is SEI in the field of audio and video development?
3D Game Modeling Learning Route
主动信息收集
3D游戏建模学习路线
电脑重装系统Win11格式化硬盘的详细方法
“2022零信任神兽方阵”启动调研,欢迎各单位填报信息
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec论文总结
The Biotin-PEG3-Br/acid/NHS ester/alcohol/amine collection that everyone wants to share
opengrok搭建[通俗易懂]
[教你做小游戏] 斗地主的手牌,如何布局?看25万粉游戏区UP主怎么说
QoS服务质量六路由器拥塞管理
哈工大软件构造Lab3(2022)
Optimizing Bloom Filter: Challenges, Solutions, and Comparisons论文总结
不止跑路,拯救误操作rm -rf /*的小伙儿
2020 ICPC Shanghai Site G
代理模式的使用总结
Redis persistence mechanism
What is the upstream bandwidth and downstream bandwidth of the server?
从 GAN 到 WGAN