领扣LintCode算法问题答案-1270. 勒索信
发布日期:2021-06-30 17:10:34 浏览次数:2 分类:技术文章

本文共 1024 字,大约阅读时间需要 3 分钟。

领扣LintCode算法问题答案-1270. 勒索信

目录

1270. 勒索信

描述

给定一个任意的表示勒索信内容的字符串,和另一个字符串表示杂志的内容,写一个方法判断能否通过剪下杂志中的内容来构造出这封勒索信,若可以,返回 true;否则返回 false。

杂志字符串中的每一个字符仅能在勒索信中使用一次。

你可以认为两个字符串都只包含小写字母。

样例 1:

输入 : ransomNote = "aa", magazine = "aab"输出 : true解析 : 勒索信的内容可以有杂志内容剪辑而来

样例 2:

输入 : ransomNote = "aaa", magazine = "aab"输出 : false解析 : 勒索信的内容无法从杂志内容中剪辑而来

题解

public class Solution {
/** * @param ransomNote: a string * @param magazine: a string * @return: whether the ransom note can be constructed from the magazines */ public boolean canConstruct(String ransomNote, String magazine) {
// Write your code here int[] counter = new int[256]; for (char c : magazine.toCharArray()) {
counter[c]++; } for (char c : ransomNote.toCharArray()) {
int count = --counter[c]; if (count < 0) {
return false; } } return true; }}

鸣谢

非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。

欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。

转载地址:https://le-yi.blog.csdn.net/article/details/108907461 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:领扣LintCode算法问题答案-1282. 翻转字符串中的元音字母
下一篇:领扣LintCode算法问题答案-1266. 找不同

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2024年04月21日 05时13分12秒