当前位置:网站首页>力扣3_383. 赎金信
力扣3_383. 赎金信
2022-07-04 21:39:00 【上课不要睡觉了】
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
示例 1:
输入:ransomNote = "a", magazine = "b"
输出:false
示例 2:
输入:ransomNote = "aa", magazine = "ab"
输出:false
示例 3:
输入:ransomNote = "aa", magazine = "aab"
输出:true
来源:力扣(LeetCode)
Java解法
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
int[] cnt = new int[26];//使用计数法
for (char c : magazine.toCharArray()) {
//ToCharArray( )的用法,将字符串对象中的字符转换为一个字符数组。
cnt[c - 'a']++;
}
for (char c : ransomNote.toCharArray()) {
cnt[c - 'a']--;
if(cnt[c - 'a'] < 0) {
return false;
}
}
return true;
}
}
Python解法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)
#这里使用了计数函数collections.Counter()不太容易理解
Python解法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)
#这里使用了replace()函数,发现一个删除一个
else:
return False
return True
边栏推荐
- Redis03 - network configuration and heartbeat mechanism of redis
- 并列图的画法,多排多列
- 能源势动:电力行业的碳中和该如何实现?
- CloudCompare&Open3D DBSCAN聚类(非插件式)
- DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
- 湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
- Which securities company is better to open an account? Is online account opening safe
- Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
- Acwing 2022 daily question
- Interview question 01.01 Determine whether the character is unique
猜你喜欢
做BI开发,为什么一定要熟悉行业和企业业务?
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
使用 BlocConsumer 同时构建响应式组件和监听状态
【LeetCode】17、电话号码的字母组合
Enlightenment of maker thinking in Higher Education
什么是商业智能(BI),就看这篇文章足够了
[weekly translation go] how to code in go series articles are online!!
玩转gRPC—深入概念与原理
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Analyzing the maker space contained in steam Education
随机推荐
Interviewer: what is XSS attack?
服装企业为什么要谈信息化?
Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
Spatiotemporal prediction 3-graph transformer
TCP协议三次握手过程
可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
Redis has three methods for checking big keys, which are necessary for optimization
Golang interview finishing three resumes how to write
VIM from dislike to dependence (23) -- the last gossip
GTEST from ignorance to proficiency (3) what are test suite and test case
Relational database
Super detailed tutorial, an introduction to istio Architecture Principle and practical application
迷失在Mysql的锁世界
1807. Replace the parentheses in the string
Caduceus从未停止创新,去中心化边缘渲染技术让元宇宙不再遥远
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
使用 BlocConsumer 同时构建响应式组件和监听状态
大厂的广告系统升级,怎能少了大模型的身影
vim 从嫌弃到依赖(23)——最后的闲扯
WebGIS框架---kalrry