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. Linux - python pip3 无法使用情况

    Linux 下使用 pip3 出了这样一个错误 error while loading shared libraries: libpython3.7m.so.1.0: cannot open shar ...

  2. python递归的最大层数?

    Python的最大递归层数是可以设置的,默认的在window上的最大递归层数是 998. 可以通过sys.setrecursionlimit()进行设置,但是一般默认不会超过3925-3929这个范围 ...

  3. 织梦dedecms会员中心分类管理无法修改、删除分类名

    member/mtypes.PHP 文件中添加 另外,member/myfriend_group.php文件中也存在同样的问题,也要添加,不添加的话好友分组中也是同样问题

  4. VS - Paginated

    BootstrapPagination.cshtml @model PaginationModel <div class="pagination"> <ul> ...

  5. DVWA-弱会话ID

    本周学习内容: 1.学习web安全深度剖析: 2.学习安全视频: 3.学习乌云漏洞: 4.总结Web应用安全权威指南: 实验内容: 进行DVWA弱会话ID实验 实验步骤: Low 1.打开DVWA,进 ...

  6. override关键字

    https://www.cnblogs.com/xinxue/p/5471708.html 2  重写 (override) 在 1.2.2 中提到 override 关键字,可以避免派生类中忘记重写 ...

  7. Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...

  8. Java 读写文件示例

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class T ...

  9. 11.linux dns服务器建立和安装apache

    dns服务器建立 1.安装bind建立dns服务器     yum install bind -y 2.安装好修改配置文件:vim /etc/named.conf 修改:      listen-on ...

  10. ubuntu之路——day4(今天主要看了神经网络的概念)

    感谢两位老师做的免费公开课: 第一个是由吴恩达老师放在网易云课堂的神经网络和深度学习,比较偏理论,使用numpy包深入浅出的介绍了向量版神经网络的处理方式,当然由于视频有点老,虽然理论很好但是工具有点 ...