当前位置:网站首页>Leetcode383 ransom letter
Leetcode383 ransom letter
2022-07-27 22:19:00 【Peihj2021】
leetcode383 Ransom letter
Topic link :leetcode
Thought analysis
The letters in the title are all lowercase characters , So we consider using arrays to map using hash algorithm , We will magazine Array mapping , Add it at the specified position 1.
And then we went to randomNote Traversal , Subtract one from the mapped position , Finally, let's see if there is less than 0 It's a good number , If there is less than 0 It proves that there are other elements in the array , That's not right , Go straight back to false, If there is no less than 0 Yes, then go back true.
Code
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] nums = new int[26];
for (int i = 0; i < magazine.length(); i++) {
nums[magazine.charAt(i) - 'a'] += 1;
}
for (int i = 0; i < ransomNote.length(); i++) {
nums[ransomNote.charAt(i) - 'a'] -= 1;
}
for (int num : nums) {
if (num < 0){
return false;
}
}
return true;
}
}
边栏推荐
- 光藕继电器
- MySQL data recovery process is based on binlog redolog undo
- Solid state relay
- Apachespark command execution (cve-2022-33891) vulnerability recurrence
- In depth understanding of recursive method calls (including instance maze problem, tower of Hanoi, monkey eating peach, fiboracci, factorial))
- [numerical analysis exercise] Jacobi iteration method of third-order matrix
- 直播软件app开发,uniapp scroll-view隐藏滚动条
- How to learn object Defineproperty | an article takes you to quickly learn
- 固体继电器
- Interview questions that big companies need to prepare
猜你喜欢

项目分析(从技术到项目、产品)

It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years

C language output teaching calendar

【图解】三次握手,四次挥手 —— 用心看这一篇就够了

An2021软件安装及基本操作(新建文件/导出)
![[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/dd/5ef46cff3988db57bfaf6fe925a0e0.jpg)
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

How to use Fiddler for weak network testing

学完4种 Redis 集群方案要多久?我一口气给你说完

极化继电器
随机推荐
Is log4j vulnerability still widespread?
2021-11-05 understand main method syntax, code block and final keyword
Excel only wants to visualize charts and make data move? Yes, come and watch (with a large number of templates to download)
软件测试的就业前景到底怎么样?
CMOS开关(二)_参数提取
Deepfake 换脸真假难辨,马斯克分克已伪装成功
固体继电器
Leetcode-39-total number of combinations
What is modcount in the source code? What's the effect
Implementation of arbitrary code execution based on.Net dynamic compilation technology
8000 word explanation of OBSA principle and application practice
Live video source code, uni app to achieve advertising scroll bar
Deepfake's face is hard to distinguish between true and false, and musk Fenke has disguised successfully
CMOS传输门原理及应用
关系型数据库的设计思想,20张图给你看的明明白白
【StoneDB故障诊断】系统资源瓶颈诊断
Is it safe to open an account online now? Then choose which securities to open a securities account
8000 word explanation of OBSA principle and application practice
Finish learning redis cluster solution at one go
ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现