当前位置:网站首页>Force buckle 3_ 383. Ransom letter
Force buckle 3_ 383. Ransom letter
2022-07-04 22:11:00 【Don't sleep in class】
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
source : Power button (LeetCode)
Java solution
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
int[] cnt = new int[26];// Use counting
for (char c : magazine.toCharArray()) {
//ToCharArray( ) Usage of , Convert a character in a string object into an array of characters .
cnt[c - 'a']++;
}
for (char c : ransomNote.toCharArray()) {
cnt[c - 'a']--;
if(cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
}
Python solution 1
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
if len(ransomNote) > len(magazine):
return False
return not collections.Counter(ransomNote) - collections.Counter(magazine)
# Here we use the counting function collections.Counter() It's not easy to understand
Python solution 2
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
for i in range(len(ransomNote)):
if ransomNote[i] in magazine:
magazine = magazine.replace(ransomNote[i],'',1)
# It's used here replace() function , One found, one deleted
else:
return False
return True
边栏推荐
猜你喜欢
凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer
Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
Redis 排查大 key 的3种方法,优化必备
Operation of adding material schedule in SolidWorks drawing
El tree combined with El table, tree adding and modifying operations
NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
[advanced C language] array & pointer & array written test questions
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient
随机推荐
You don't have to run away to delete the library! Detailed MySQL data recovery
Case sharing | integrated construction of data operation and maintenance in the financial industry
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
The drawing method of side-by-side diagram, multi row and multi column
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
挖财学院股票开户安全吗?开户只能在挖财开户嘛?
面试题 01.01. 判定字符是否唯一
Acwing 2022 daily question
Representation of confidence interval
How much is the minimum stock account opening commission? Is it safe to open an account online
使用 BlocConsumer 同时构建响应式组件和监听状态
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
Relational database
HBuilder X 常用的快捷键
HDU - 1078 fatmouse and cheese (memory search DP)
TCP protocol three times handshake process
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
close系统调用分析-性能优化
PostgreSQL基本结构——表