RansomNote
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
1 public class Solution {
2 public boolean canConstruct(String ransomNote, String magazine) {
3 int[] arr = new int[26];
4 for (int i = 0; i < magazine.length(); i++) {
5 arr[magazine.charAt(i) - 'a']++;
6 }
7 for (int i = 0; i < ransomNote.length(); i++) {
8 if(--arr[ransomNote.charAt(i)-'a'] < 0) {
9 return false;
10 }
11 }
12 return true;
13 }
14 }
RansomNote的更多相关文章
- ransom-note
https://leetcode.com/problems/ransom-note/ public class Solution { public boolean canConstruct(Strin ...
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- LeetCode之383. Ransom Note
-------------------------------------------- 思路就是进行频率统计. 统计一下第二个字符串字符出现次数++统计一下第一个字符串中字符出现次数--如果出现负数 ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode: Ransom Note
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] rans ...
- String Start!
(1)Ransom Note 解题思路: 题目叫做Ransom Note,勒索信.勒索信,为了不暴露字迹,就从杂志上搜索各个需要的字母,组成单词来表达的意思.这样来说,题目也就清晰了,判断杂志上的字是 ...
- LeetCode:Ransom Note_383
LeetCode:Ransom Note [问题再现] Given an arbitrary ransom note string and another string contai ...
随机推荐
- IE环境下判断IE版本的语句...[if lte IE 6]……[endif][if lte IE 7]……[endif]
<!--[if IE 6]> <![endif]--> 只有IE6版本可见 <!--[if lte IE 6]> <![endif]--> IE6及其以 ...
- 5.3监听请求:使用eclipse的tcp/ip工具(端口转换)
1.改用wsdl文件生成响应文件 运行浏览器输入发布的地址,获得wsdl源码保存在项目路径下, 2.创建接口转换器,window-property-tcpip 客户端执行结果:
- SNMP
net-snmp 了解snmp程序最好的工具,snmpwalk和snmptable都是关键命令,举例: snmptable -v 2c -c public X.X.X.X ifTable 显示网络接口 ...
- FC400的PM2.5过滤效率测试。。
今天北京雾霾爆表,拿空气堡在外面测试,达到600多,然后在新风风口测试,差不多60到80.可以得到效率只有85%到90%左右,这和销售说的数值差不多,肯定达不到手册上写的99%,那是实验值,骗人的. ...
- PMP考试
今天是第二次PMP模拟考试,得了146分,比上次高25分,这次题目相对简单些,看来昨晚的复习没有白费,还是有效果的. 有些题目影响还是比较深刻,老外的项目管理思想是先规划好一切再执行(管理),比如信息 ...
- 【转】sql各种查询技巧
高级查询在数据库中用得是最频繁的,也是应用最广泛的. Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from ...
- Java反射遍历数组
日志中有时候需要查看数组中的值,但是重载很多的打印函数,觉得很别扭.所以想通过反射,获取数组中的值,打印出来.Java提供了数组反射操作的类,之前没有关注过,提供的方法简单易用. public sta ...
- Upload Images
ASP.NET图片批量上传,可预览带进度条 http://www.okbase.net/file/item/5492
- [已解决] C3p0连接配置
#用户名 c3p0.user=test c3p0.user=root # 用户密码--> c3p0.password=test c3p0.password=root c3p0.driverCla ...
- mysql分区交换exchange partition
在表和分区间交换数据 mysql5.6开始支持alter table..exchange partition语法,该语句允许分区或子分区中的数据与另一个非分区的表中的数据进行交换,如果非分区表中的数据 ...