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 ...
随机推荐
- zzuli-小火山的跳子游戏
Description 小火山和火山火山在一块玩跳子游戏.规则如下: 1:跳子的起始位置为0,棋盘大小从1到N 2:每次跳子跳k步. 例如当前位置为i, 那么下一步为i + k 3:跳 ...
- mac下使用gcc
1.下载安装macports:http://www.macports.org/install.php 安装完成之后,打开终端 2.在终端中输入 port install gcc_select 3.使用 ...
- Your intuition 你的直觉
If you’re thinking just like everyone else, you aren’t really thinking. Follow your intuition. Do wh ...
- Scala基础语法
/* 学慕课网上<Scala程序设计>课程跟着敲的代码 作为代码参考也是很好的 在scala_ide.org上下载eclipse IDE,新建一个worksheet,就可以像在xcode的 ...
- Core Data 使用映射模型
Core Data 使用映射模型 如果新版本的模型存在较复杂的更改,可以创建一个映射模型,通过该模型指定源模型如何映射到目标模型. 创建映射模型,新建File, Core Data 选择Mappin ...
- [已解决] github merge指定commit
比如说有两个branch,分别是master和m1,我们在m1上修改的bug怎么merge到master上呢, 怎么merge我不知道,但是有另外一个命令可以做到,比如m1做commit,sha-1为 ...
- [解决方案]vs2015无法解析外部符号__imp__fprintf和__imp____iob_func
转自:http://www.cnblogs.com/ubosm/p/5444919.html 使用vs2015编译ffmpeg的一个小项时,出现了__imp__fprintf和__imp____iob ...
- android微信支付总结+自己搭建服务器
1.前期注册操作 1-1:微信开发平台:https://open.weixin.qq.com/ 1-2:创建移动应用 签名获取: 1.将自己的apk签名打包,运行到手机上. 2.将微信支付的签名工具, ...
- css 水平垂直居中
主要是垂直居中有点麻烦,以下代码可以实现,先记下来: <style type="text/css"> div{ border:1px solid #ccc; heigh ...
- Javascript > Eclipse > problems encountered during text search
Reproduce: Ctrl + H, Select "File Search", will encounter eclipse kinds of bug/error alert ...