当前位置:网站首页>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
边栏推荐
- NAACL-22 | 在基于Prompt的文本生成任务上引入迁移学习的设置
- 时空预测3-graph transformer
- Machine learning notes mutual information
- 面试题 01.08. 零矩阵
- GTEST from ignorance to proficient use (2) what is test fixture
- Spatiotemporal prediction 3-graph transformer
- Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
- 传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
- 什么是商业智能(BI),就看这篇文章足够了
- 服装企业为什么要谈信息化?
猜你喜欢
Case sharing | integrated construction of data operation and maintenance in the financial industry
Zhiyang innovation signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry
close系统调用分析-性能优化
Scala下载和配置
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
Redis03 - network configuration and heartbeat mechanism of redis
TCP协议三次握手过程
[optimtool.unconstrained] unconstrained optimization toolbox
抖音实战~评论数量同步更新
随机推荐
Why should garment enterprises talk about informatization?
开户哪家券商比较好?网上开户安全吗
历史最全混合专家(MOE)模型相关精选论文、系统、应用整理分享
File read write
new IntersectionObserver 使用笔记
网上开户哪家证券公司佣金最低,我要开户,网上开户安全吗
Bookmark
# 2156. 查找给定哈希值的子串-后序遍历
Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei
Interview question 01.08 Zero matrix
机器人相关课程考核材料归档实施细则2022版本
HDU - 2859 Phalanx(DP)
Redis 排查大 key 的3种方法,优化必备
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
力扣3_383. 赎金信
复数在数论、几何中的用途 - 曹则贤
并发网络模块化 读书笔记转
面试题 01.01. 判定字符是否唯一
i. Mx6ull driver development | 24 - platform based driver model lights LED
TLA+ 入门教程(1):形式化方法简介