已知两个字符串,然后比较一个字符串是否来自另一个字符串,没有顺序要求。

简单题,用一个数组保存前一个字符串的每一个字符出现的次数,然后循环后一个字符串去检查,如果次数不够了,那么就返回false

public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[26];
for (int i = 0; i < magazine.length(); i++) {
arr[magazine.charAt(i) - 'a']++;
}
for (int i = 0; i < ransomNote.length(); i++) {
if(--arr[ransomNote.charAt(i)-'a'] < 0) {
return false;
}
}
return true;
}
}

leetcode383的更多相关文章

  1. 【算法训练营day7】LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和

    [算法训练营day7]LeetCode454. 四数相加II LeetCode383. 赎金信 LeetCode15. 三数之和 LeetCode18. 四数之和 LeetCode454. 四数相加I ...

  2. [Swift]LeetCode383. 赎金信 | Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  3. LeetCode383. 赎金信

    题目 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成.如果可以构成,返回 tru ...

  4. LeetCode通关:哈希表六连,这个还真有点简单

    精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...

随机推荐

  1. lucene 索引删除

    1.IndexWriter和IndexReader都有删除索引的方法:deleteDocuments(); 不建议使用IndexReader删除索引:使用IndexReader进行删除时,必须关闭所有 ...

  2. linux提示语言包

    locale -a 查看可用的语言包.选择中文语言包 echo 'LC_ALL="zh_CN.utf8"' >> /etc/profileecho 'export LC ...

  3. Maven中央仓库地址

    Maven 中央仓库地址: 1. http://www.sonatype.org/nexus/ 2. http://mvnrepository.com/ (本人推荐仓库) 3. http://repo ...

  4. LeetCode 之 Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. LOOPS

    LOOPS 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3853 递推 设dp[i][j]为(i,j)到终点期望的使用魔力值,mp[i][ ...

  6. 第八十一节,CSS3变形效果

    CSS3变形效果 学习要点: 1.transform 2.transform-origin 3.浏览器版本 本章主要探讨HTML5中CSS3的变形效果,通过变形效果,可以平移.缩放和旋转元素的功能.  ...

  7. 《JS权威指南学习总结--6.9序列化对象》

    内容要点: 一.JSON.stringify()和JSON.parse() 1.对象序列化(serialization)是指将对象的状态转换为字符串,也可将字符串还原为对象.ES5提供了内置函数JSO ...

  8. spring security maven dependency

    Unable to locate Spring NamespaceHandler for XML schema namespace [ spring secutity dependency: < ...

  9. 做一个视频播放器在没开始播放的时候有一张图片实际上就是拿一张图片盖住视频承载的屏幕当出发。play的时候图片隐藏 img

    saxda 某个元素.style.class='';也可以是.className <!DOCTYPE html><html lang="en"><he ...

  10. (转)了解JNDI

    JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的意义和作用,就没有 ...