当前位置:网站首页>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
边栏推荐
- É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)
- Is it safe to open an account in the stock of Caicai college? Can you only open an account by digging money?
- WebGIS框架---kalrry
- 并发网络模块化 读书笔记转
- HUAWEI nova 10系列发布 华为应用市场筑牢应用安全防火墙
- B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
- [optimtool.unconstrained] unconstrained optimization toolbox
- gtest从一无所知到熟练使用(2)什么是测试夹具/装置(test fixture)
- Application practice | Shuhai supply chain construction of data center based on Apache Doris
- 机器人相关课程考核材料归档实施细则2022版本
猜你喜欢
Keep on fighting! The city chain technology digital summit was grandly held in Chongqing
玩转gRPC—深入概念与原理
Play with grpc - go deep into concepts and principles
AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
Energy momentum: how to achieve carbon neutralization in the power industry?
el-tree结合el-table,树形添加修改操作
Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient
TLA+ 入门教程(1):形式化方法简介
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
How to remove the black dot in front of the title in word document
随机推荐
【Acwing】第58场周赛 题解
The use of complex numbers in number theory and geometry - Cao Zexian
DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
gtest从一无所知到熟练使用(3)什么是test suite和test case
What is business intelligence (BI), just look at this article is enough
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
Scala下载和配置
常用的开源无代码测试工具
做BI开发,为什么一定要熟悉行业和企业业务?
Representation of confidence interval
WebGIS框架---kalrry
i. Mx6ull driver development | 24 - platform based driver model lights LED
从RepVgg到MobileOne,含mobileone的代码
力扣2_1480. 一维数组的动态和
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
Case sharing | integrated construction of data operation and maintenance in the financial industry
Spatiotemporal prediction 3-graph transformer
[optimtool.unconstrained] unconstrained optimization toolbox
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
QT—双缓冲绘图