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 ...
随机推荐
- 浅谈js回调函数
回调函数原理: 我现在出发,到了通知你”这是一个异步的流程,“我出发”这个过程中(函数执行),“你”可以去做任何事,“到了”(函数执行完毕)“通知你”(回调)进行之后的流程 例子 1.基本方法 ? 1 ...
- HTML5--拖动01
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- web前端浮动、清浮动问题
浮动的问题如下一一列举如有考虑不周的欢迎欢迎大家补充: 1.浮动,兼容性问题3px的问题,双边距的问题 在平时工作的过程中,解决3px的问题,一般都是初始化*{margin:0;padding:0px ...
- 学习笔记:MySQL字符串类型
字符串类型 a) char和varchar 1.都需要指定字符的长度,char中的长度是字符的长度,而varchar的长度是字节的长度 2. char中指定的长度就是实际占用的长度,而 ...
- JVM基础知识
JVM简介 JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的.J ...
- HDU 2732:Leapin' Lizards(最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=2732 题意:给出两个地图,蜥蜴从一个柱子跳跃到另外一个地方,那么这个柱子就可能会坍塌,第一个地图是柱子可以容忍跳 ...
- python:点赞功能
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- python:模态编程框
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 【转】SVN添加文件时的错误处理:...\conf\svnserve.conf:12: Option expected
转载地址:http://www.linuxidc.com/Linux/2014-09/106683.htm 安装完SVN服务器,添加完用户权限后,准备将本地的项目add到服务器上时,报"C: ...
- funsioncharts的图表操作heatmap
网址:http://www.fusioncharts.com/dev/chart-guide/heat-map-chart/introduction.html 以下只是假数据,目前还没有实现动态数据获 ...