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 ...
随机推荐
- AngularJs自定义指令--执行顺序 (原文:http://www.cnblogs.com/sagacite/p/4624227.html)
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- java语言的I/O操作预习
一些概念: 流:计算机的输入输出之间流动的数据序列,也是类的对象.java中的流方式就像是建立在数据交换源和目的之间的一条通信路径. 数据源:计算机中的数据源是指可以提供数据的地方,包括键盘,磁盘文件 ...
- Winform-DataGridView 实现如Excel的粘贴复制
void AddDataGridView(DataGridView gridView, string s) { s = s.Replace("/", @"\") ...
- Android Bootloader LittleKernel的两篇文章 【转】
转自:http://blog.csdn.net/loongembedded/article/details/41747523 2014-12-05 14:37 3599人阅读 评论(2) 收藏 举报 ...
- iOS AFNetworking HTTPS 认证
HTTPS 中双向认证SSL 协议的具体过程: 这里总结为详细的步骤: ① 浏览器发送一个连接请求给安全服务器. ② 服务器将自己的证书,以及同证书相关的信息发送给客户浏览器. ③ 客户浏览器检查服务 ...
- [转]opencv3.0 鱼眼相机标定
[原文转自]:http://blog.csdn.net/qq_15947787/article/details/51441031 前两天发表的时候没注意,代码出了点错误,所以修改了一下,重新发上来. ...
- Spring源代码解析
Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.itey ...
- nodejs学习笔记一:安装express框架并构建工程目录
偶遇node是在一个阳光明媚的上午,无意间打开博客看到一片关于nodejs的介绍,通读全篇后,心情跌宕起伏,哎呀,这么好的东西我竟然现在才知道,这是最气的,于是马上开始制定学习nodejs计划,好了, ...
- IOS 截取图片 部分 并生成新图片
/** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...
- sql查看锁与解锁
select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran ...