[LeetCode]--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


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

我的想法是用map记录两边的字母表的每一个字母的个数,然后判断ransomNote里面字母肯定都要包含在magazine里面,而且个数要比它少。

public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> mapRan = new HashMap<Character, Integer>();
        Map<Character, Integer> mapMag = new HashMap<Character, Integer>();

        for (int i = 0; i < ransomNote.length(); i++)
            if (!mapRan.containsKey(ransomNote.charAt(i)))
                mapRan.put(ransomNote.charAt(i), 1);
            else
                mapRan.put(ransomNote.charAt(i),
                        mapRan.get(ransomNote.charAt(i)) + 1);

        for (int i = 0; i < magazine.length(); i++)
            if (!mapMag.containsKey(magazine.charAt(i)))
                mapMag.put(magazine.charAt(i), 1);
            else
                mapMag.put(magazine.charAt(i),
                        mapMag.get(magazine.charAt(i)) + 1);

        for (Character c : mapRan.keySet()) {
            if (!mapMag.containsKey(c) || mapRan.get(c) > mapMag.get(c))
                return false;
        }
        return true;
    }

这是LeetCode Discuss中的最热代码,它的原理就是列出了magazine的字母表,然后算出了出现个数,然后遍历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) {
                return false;
            }
        }
        return true;
    }
目录
相关文章
LeetCode 383. Ransom Note
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。
53 0
LeetCode 383. Ransom Note
LeetCode之Ransom Note
LeetCode之Ransom Note
98 0
|
Java
[LeetCode] Ransom Note
A very typical application of hash maps. Since I am now learning Java, I code in Java. The following code uses toCharArray() and getOrDefault(), which are learnt from this post.
899 0
|
18天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-2
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
18天前
|
算法 C++
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题-1
【数据结构与算法】:关于时间复杂度与空间复杂度的计算(C/C++篇)——含Leetcode刷题
|
19天前
|
索引
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
【LeetCode刷题】二分查找:山脉数组的峰顶索引、寻找峰值
|
19天前
|
算法
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)
【LeetCode刷题】滑动窗口解决问题:串联所有单词的子串(困难)、最小覆盖子串(困难)