当前位置:网站首页>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
边栏推荐
- 案例分享|金融业数据运营运维一体化建设
- 机器人相关课程考核材料归档实施细则2022版本
- 开户哪家券商比较好?网上开户安全吗
- PMO:比较25种分子优化方法的样本效率
- 283. Moving zero-c and language assisted array method
- [optimtool.unconstrained] unconstrained optimization toolbox
- GTEST from ignorance to proficiency (4) how to write unit tests with GTEST
- 机器学习笔记 - 互信息Mutual Information
- AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
- 面试题 01.08. 零矩阵
猜你喜欢

Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies

能源势动:电力行业的碳中和该如何实现?

MongoDB聚合操作总结

迷失在Mysql的锁世界

Super detailed tutorial, an introduction to istio Architecture Principle and practical application

Case sharing | integrated construction of data operation and maintenance in the financial industry

Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development

卷积神经网络模型之——LeNet网络结构与代码实现

常用的开源无代码测试工具
【米哈游2023届秋招】开启【校招唯一专属内推码EYTUC】
随机推荐
TCP协议三次握手过程
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
Sorting and sharing of selected papers, systems and applications related to the most comprehensive mixed expert (MOE) model in history
HDU - 1078 FatMouse and Cheese(记忆化搜索DP)
凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
电话加密,中间4为****代替
ACM Multimedia 2022 | 视觉语言预训练模型中社会偏见的反事实衡量和消除
抖音实战~评论数量同步更新
做BI开发,为什么一定要熟悉行业和企业业务?
保证接口数据安全的10种方案
New intersectionobserver usage notes
我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
股票开户流程是什么?使用同花顺手机炒股软件安全吗?
Kdd2022 | what features are effective for interaction?
机器人相关课程考核材料归档实施细则2022版本
删库不必跑路!详解 MySQL 数据恢复
QT - double buffer plot
Redis03 - network configuration and heartbeat mechanism of redis
WebGIS framework -- kalrry