Algorithm


Problem Name: 383. Ransom Note

Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

 

Example 1:

Input: ransomNote = "a", magazine = "b"
Output: false

Example 2:

Input: ransomNote = "aa", magazine = "ab"
Output: false

Example 3:

Input: ransomNote = "aa", magazine = "aab"
Output: true

 

Constraints:

  • 1 <= ransomNote.length, magazine.length <= 105
  • ransomNote and magazine consist of lowercase English letters.
 

Code Examples

#1 Code Example with C Programming

Code - C Programming


bool canConstruct(char* ransomNote, char* magazine) {
    int i, t = 0, n[26] = { 0 };
    
    while (ransomNote && *ransomNote) {
        i = *ransomNote - 'a';
        n[i] ++;
        t ++;
        ransomNote ++;
    }
    
    while (magazine && *magazine) {
        i = *magazine - 'a';
        if (n[i]) {
            n[i] --;
            t --;
        }
        magazine ++;
    }
    
    if (t) return false;
    
    return true;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
ransomNote = "a", magazine = "b"

Output

x
+
cmd
false

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean canConstruct(String ransomNote, String magazine) {
    int[] count = new int[26];
    for (char c : magazine.toCharArray()) {
      count[c - 'a']++;
    }
    for (char c: ransomNote.toCharArray()) {
      if (count[c - 'a'] == 0) {
        return false;
      }
      count[c - 'a']--;
    }
    return true;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
ransomNote = "a", magazine = "b"

Output

x
+
cmd
false

#3 Code Example with Javascript Programming

Code - Javascript Programming


const canConstruct = function(ransomNote, magazine) {
  const m = new Map()
  for(let c of magazine) {
    m.set(c, (m.get(c) || 0) + 1 )
  }
  for(let c of ransomNote) {
    if(!m.has(c) || m.get(c) <= 0) return false
    m.set(c, m.get(c) - 1>
  }
  return true
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
ransomNote = "aa", magazine = "ab"

Output

x
+
cmd
false

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        cnt = collections.Counter(magazine)
        for c in ransomNote:
            if cnt[c]:
                cnt[c] -= 1
            else:
                return False
        return True
Copy The Code & Try With Live Editor

Input

x
+
cmd
ransomNote = "aa", magazine = "ab"

Output

x
+
cmd
false

#5 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0383_RansomNote
    {
        public bool CanConstruct(string ransomNote, string magazine)
        {
            var counts = new int[26];
            foreach (var ch in magazine)
                counts[ch - 'a']++;

            foreach (var ch in ransomNote)
            {
                if (counts[ch - 'a'] == 0)
                    return false;
                counts[ch - 'a']--;
            }

            return true;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
ransomNote = "aa", magazine = "aab"

Output

x
+
cmd
true
Advertisements

Demonstration


Previous
#381 Leetcode Insert Delete GetRandom O(1) - Duplicates allowed in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#384 Leetcode Shuffle an Array Solution in C, C++, Java, JavaScript, Python, C# Leetcode