当前位置:网站首页>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.
边栏推荐
- 【二叉树】二叉搜索树的后序遍历序列
- 多功能纳米酶Ag/PANI|柔性衬底纳米ZnO酶|铑片纳米酶|Ag-Rh合金纳米颗粒纳米酶|铱钌合金/氧化铱仿生纳米酶
- @Autowired annotation --required a single bean, but 2 were found causes and solutions
- Transferrin-modified vincristine-tetrandrine liposomes | transferrin-modified co-loaded paclitaxel and genistein liposomes (reagents)
- 水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子
- Hangdian Multi-School Seven 1003-Counting Stickmen (Combination Mathematics)
- 代理模式的使用总结
- 这7个自动化办公模版 教你玩转表格数据自动化
- 一维数组动态和问题答记
- 转铁蛋白修饰蛇床子素长循环脂质体/负载三七皂苷R1的PEG-PLGA纳米粒([email protected] NPs)
猜你喜欢
leetcode 85.最大矩形 单调栈应用
(十二) findContours函数的hierarchy详解
Introduction to 3 d games beginners essential 】 【 modeling knowledge
Site Architecture Detection & Chrome Plugin for Information Gathering
【知识分享】在音视频开发领域中SEI到底是个啥?
- [email protected])纳米酶"/>
血红素-金纳米颗粒(Heme-AuNP)复合纳米酶|金纳米颗粒核多孔空心碳纳米球壳([email protected])纳米酶
QoS服务质量八拥塞避免
网站架构探测&chrome插件用于信息收集
Keras深度学习实战(17)——使用U-Net架构进行图像分割
[Teach you how to do mini-games] How to lay out the hands of Dou Dizhu?See what the UP master of the 250,000 fan game area has to say
随机推荐
[CNN] Brush SOTA's trick
转铁蛋白修饰蛇床子素长循环脂质体/负载三七皂苷R1的PEG-PLGA纳米粒([email protected] NPs)
赎金信问题答记
常见端口及服务
今日份bug,点击win10任务栏视窗动态壁纸消失的bug,暂未发现解决方法。
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
[教你做小游戏] 只用几行原生JS,写一个函数,播放音效、播放BGM、切换BGM
【知识分享】在音视频开发领域中SEI到底是个啥?
laya打包发布apk
Metasploit——渗透攻击模块(Exploit)
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec Paper Summary
Optimization is a habit The starting point is to 'stand close to the critical'
redis如何查看key的有效期
Solution for thread not gc-safe when Rider debugs ASP.NET Core
QoS Quality of Service Seven Switch Congestion Management
【SemiDrive源码分析】【MailBox核间通信】51 - DCF_IPCC_Property实现原理分析 及 代码实战
【无标题】基于Huffman和LZ77的GZIP压缩
[SemiDrive source code analysis] [MailBox inter-core communication] 52 - DCF Notify implementation principle analysis and code combat
GBASE 8s 高可用RSS集群搭建
(12) findContours function hierarchy explanation