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.
  • Examples:
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
  • Analysis:
Use a map to count the number of all characters in the magazine.

Match the map with all characters of ransomNote.

If miss matched, return false. Otherwise , true
  • Code - Java:
public class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        Map<Character, Integer> counts = new HashMap<Character, Integer>();
        int rLen = ransomNote.length();
        int mLen = magazine.length();
        for (int i = 0; i < mLen; i++) {
            char ch = magazine.charAt(i);
            int count = counts.getOrDefault(ch, 0) + 1;
            counts.put(ch, count);
        }
        for (int i = 0; i < rLen; i++) {
            char ch = ransomNote.charAt(i);
            int count = counts.getOrDefault(ch, 0) - 1;
            if (count < 0) {
                return false;
            }
            counts.put(ch, count);
        }
        return true;
    }
}
  • Code - C++:
class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        map<char, int> counts;
        int rLen = ransomNote.length();
        int mLen = magazine.length();
        for (int i = 0; i < mLen; i++) {
            char ch = magazine[i];
            if (counts.find(ch) != counts.end()) {
                counts[ch] += 1;
            } else {
                counts.insert(pair<char, int>(ch, 1));
            }
        }
        for (int i = 0; i < rLen; i++) {
            char ch = ransomNote[i];
            if (counts.find(ch) == counts.end()) {
                return false;
            }
            counts[ch] -= 1;
            if (counts[ch] < 0) {
                return false;
            }
        }
        return true;
    }
};
  • Code - Python:
class Solution(object):
    def canConstruct(self, ransomNote, magazine):
        """
        :type ransomNote: str
        :type magazine: str
        :rtype: bool
        """
        counts = {}
        rLen, mLen = len(ransomNote), len(magazine)
        for ch in magazine:
            if ch in counts:
                counts[ch] += 1
            else:
                counts[ch] = 1
        for ch in ransomNote:
            if ch not in counts:
                return False
            counts[ch] -= 1
            if counts[ch] < 0:
                return False
        return True
  • Time Complexity: O(M + N), M is the length fo ransomNote, N is the length of magazine

  • Space Complexity: O(N), N is the length of magazine

results matching ""

    No results matching ""