LeetCode_383. Ransom Note
383. Ransom Note
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的更多相关文章
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- Ransom Note(383)
题目:Given an arbitrary ransom note string and another string containing letters from all the magazine ...
- [Swift]LeetCode383. 赎金信 | Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- leetcode之Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from a ...
随机推荐
- 《了解python》
编程语言的发展史: 1.机器语言 站在计算机角度,直接用二进制跟计算机直接沟通,直接操控硬件 优点:计算机能够直接读懂,运行速度快 缺点:开发效率低 2.汇编语言 站在计算机角度,用简单的英文标签标识 ...
- netty: 编解码之jboss marshalling, 用marshalling进行对象传输
jboss marshalling是jboss内部的一个序列化框架,速度也十分快,这里netty也提供了支持,使用十分方便. TCP在网络通讯的时候,通常在解决TCP粘包.拆包问题的时候,一般会用以下 ...
- How To Set The Hostname On Ubuntu Or Debian?
$ sudo hostnamectl set-hostname your-hostname $ sudo vim /etc/hosts Open the hosts file and add the ...
- [C++11]C++可变参数模板
可变参数模板 原文链接: http://blog.csdn.net/xiaohu2022/article/details/69076281 普通模板只可以采取固定数量的模板参数.然而,有时候我们希望模 ...
- 影像优化 OptimizeRaster工具包介绍
Esri OptimizeRasters是一个高效.可配置的开源工具包. OptimizeRasters提供了以下功能: 影像格式转换和压缩.支持输出优化栅格格式:MRF.分块TIFF.云存储优化Ge ...
- Comet OJ - Contest #8题解
传送门 \(A\) 咕咕咕 const int N=1005; char s[N][N];int len[N],n,id; inline bool cmp(R int j,R int k){ R in ...
- SweetAlert 2 全网最详细的使用方法
官网链接 SweetAlert2 官网链接 准备阶段 CDN js 如果该 链接 时间久远了 , 可以在官网去找找最新的 可以把 js 复制出来 自己新建一个文件 然后 引用到 html 中 1. 带 ...
- Hadoop 压缩
压缩的好处 文件压缩的好处:减少文件存储锁需要的磁盘空间,加速数据在网络和磁盘上的传输. 常见的压缩格式 压缩格式 工具 算法 文件扩展名 是否可以切分 DELATE 无 DEFLATE .d ...
- 配置Windows实例NTP服务
本文介绍如何开启和配置Windows NTP服务,保证实例本地时间精确同步. Windows实例NTP服务介绍 目前,所有地域下ECS实例默认采用CST(China Standard Time)时区, ...
- Android.mk文件LOCAL_MODULE_TAGS 说明
在移植wireless_tools驱动的时候发现居然没去编译咱的代码,奇怪,后来发现只有LOCAL_MODULE_TAGS 选项这个最有可疑,后来发现有这个说法 LOCAL_MODULE_TAGS : ...