当前位置:网站首页>[leetcode 383] ransom letter
[leetcode 383] ransom letter
2022-04-23 06:23:00 【Don't steal my energy】
Topic introduction
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 .
Example 1
Input :ransomNote = "a", magazine = "b"
Output :false
Example 2
Input :ransomNote = "aa", magazine = "ab"
Output :false
Example 3
Input :ransomNote = "aa", magazine = "aab"
Output :true
Tips
- 1 <= ransomNote.length, magazine.length <= 105
- ransomNote and magazine It's made up of lowercase letters
Ideas
The idea and of this question 【LeetCode 350 Intersection of two arrays II】 10 Fen is the same , The core is to use the idea of hash table . take magazine The letters in and the number of times they appear are stored separately , And then go through ransomNote Letters in , Judge , If the number of occurrences >0, The number of times it appears -1, Traverse the next letter ; If the number of occurrences ==0, shows ransomNote Can it be done by magazine The characters inside make up , return flase.
1. One dimensional array implementation
bool canConstruct(string ransomNote, string magazine) {
int a[123]={
0};// Small letters z Of ascii value 122
for(char x:magazine)
a[int(x)]++;
for(char y:ransomNote){
if(a[int(y)]>0)
a[int(y)]--;
else
return 0;
}
return 1;
}
2. Hash table implementation
bool canConstruct(string ransomNote, string magazine){
unordered_map<char,int>a; // take magazine The letters in and their occurrences are stored separately
for(char x:magazine)
a[x]++;
for(char y:ransomNote){
if(a[y]>0)
a[y]--;
else
return 0;
}
return 1;
}
On memory consumption , The array implementation is lower than the hash table implementation .
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012053.html
边栏推荐
- Example of ticket selling with reentrant lock
- Traitement des séquelles du flux de Tensor - exemple simple d'enregistrement de torche. Utils. Données. Dataset. Problème de dimension de l'image lors de la réécriture de l'ensemble de données
- MySQL occasional Caton
- 檢測技術與原理
- How to grow at work
- A sharp tool to improve work efficiency
- Best practices for MySQL storage time
- SQL optimization best practices
- 8. Integer Decomposition
- Optional best practices
猜你喜欢
Filebrowser realizes private network disk
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Guaba and Computational Geometry
A sharp tool to improve work efficiency
Fundamentals of digital image processing (Gonzalez) I
String notes
The user name and password of users in the domain accessing the samba server outside the domain are wrong
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
Create binary tree
Understanding and installing MySQL
随机推荐
Exception handling: grab and throw model
PyTorch笔记——通过搭建ResNet熟悉网络搭建方式(完整代码)
POI and easyexcel exercises
线代第四章-向量组的线性相关
The attendance client date of K / 3 wise system can only be selected to 2019
C3p0 database connection pool usage
String notes
Substring Inversion (Easy Version)
Kibana search syntax
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
Calculation (enter the calculation formula to get the result)
Framework analysis 2 Source code - login authentication
Detection technology and principle
Collections multiple parameter sorting
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Fundamentals of digital image processing (Gonzalez) I
Rainbow (DP)
Event listener
SQL optimization best practices
Algèbre linéaire chapitre 1 - déterminants