383. Ransom Note

Easy

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
package leetcode.easy;

public class RansomNote {
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) {
arr[ransomNote.charAt(i) - 'a']--;
} else {
return false;
}
}
return true;
} @org.junit.Test
public void test() {
System.out.println(canConstruct("a", "b"));
System.out.println(canConstruct("aa", "ab"));
System.out.println(canConstruct("aa", "aab"));
}
}

LeetCode_383. Ransom Note的更多相关文章

  1. [LeetCode] Ransom Note 赎金条

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  2. leetcode 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  3. 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  4. leetcode修炼之路——383. Ransom Note

    题目是这样的 Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

  5. 14. leetcode 383. Ransom Note

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

  6. Ransom Note(383)

    题目:Given an arbitrary ransom note string and another string containing letters from all the magazine ...

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

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

  8. [LeetCode&Python] Problem 383. Ransom Note

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

  9. leetcode之Ransom Note

    题目描述: 
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 a ...

随机推荐

  1. 2019-2020-1 20199312 《Linux内核原理与分析》 第八周作业

    ELF(Executable and Linkable Format)可执行的和可链接的格式.(对应Windows为PE) 其包含了以下三类: 可重定位文件:保存着代码和适当的数据,用来和其它的目标文 ...

  2. Go读写文件

    Go序列化和反序列化 package main import ( "bufio" "encoding/json" "fmt" "o ...

  3. 微信公众号调用外部浏览器打开指定URL链接是如何实现的

    在涉及移动端支付的项目时,由于对支付需求的精细化,不仅需要扫码支付,还有唤醒App支付,另外还有在微信.QQ.支付宝内置浏览器给出相应的提示. 好在国内各大巨头公司在开发浏览器的时候都在浏览器标识上加 ...

  4. zabbix基本介绍

    来源是 觅安教育 大家有兴趣可以去哔哩哔哩搜搜. Open-falcon是由小米公司开源 比如windows,linux,unix,openBSD,AIX,solaris,Mac等操作系统,都可以安装 ...

  5. 鼠标经过图片会移动(css3过渡,overflow:hidden)

    效果图如下: 代码: <body> <div><img src="jd.jpg"></div> </body> img{ ...

  6. CSS3 新增选择器:伪类选择器和属性选择器

    一.结构(位置)伪类选择器( : ) 1.:first-child 2.:last-child 3.:nth-child(n)或者:nth-child(2n)或者:nth-child(2n+1) &l ...

  7. 【概率论】5-10:二维正态分布(The Bivariate Normal Distributions)

    title: [概率论]5-10:二维正态分布(The Bivariate Normal Distributions) categories: - Mathematic - Probability k ...

  8. GoCN每日新闻(2019-09-29)

    1. 干货满满的Go Modules和goproxy.cn https://juejin.im/post/5d8ee2db6fb9a04e0b0d9c8b 2. gnet: 一个轻量级且高性能的 Go ...

  9. 解释下Http请求头和常见响应状态码

    Accept-Charset:指出浏览器可以接受的字符编码.英文浏览器的默认值是ISO-8859-1.ccept:指浏览器或其他客户可以接爱的MIME文件格式.可以根据它判断并返回适当的文件格式. A ...

  10. 数据结构Java版之深度优先-图(十二)

    这里用深度优先遍历存在矩阵里面的图. 深度优先利用的是栈的FIFO特性.为此遍历到底后,可以找到最相邻的节点继续遍历.实现深度优先,还需要在节点加上一个访问标识,来确定该节点是否已经被访问过了. 源码 ...