Arena / Problems /

383. Ransom Note

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #383

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 <= 10^5
  • ransomNote and magazine consist of lowercase English letters.
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        var charToCount = new HashMap<Character, Integer>();

        for (var character : magazine.toCharArray()) {
            charToCount.put(character, charToCount.getOrDefault(character, 0) + 1);
        }

        for (var character : ransomNote.toCharArray()) {
            charToCount.put(character, charToCount.getOrDefault(character, 0) - 1);

            if (charToCount.get(character) < 0) {
                return false;
            }
        }

        return true;
    }
}
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

# from collections import defaultdict

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        char_to_count = defaultdict(int)

        for char in magazine:
            char_to_count[char] += 1

        for char in ransomNote:
            char_to_count[char] -= 1

            if char_to_count[char] < 0:
                return False

        return True
⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

var canConstruct = function (ransomNote, magazine) {
  const charToCount = new Map()

  for (const character of magazine) {
    charToCount.set(character, (charToCount.get(character) || 0) + 1)
  }

  for (const character of ransomNote) {
    charToCount.set(character, (charToCount.get(character) || 0) - 1)

    if (charToCount.get(character) < 0) {
      return false
    }
  }

  return true
};
⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

public class Solution
{
    public bool CanConstruct(string ransomNote, string magazine)
    {
        var charToCount = new Dictionary<char, int>();

        foreach (char character in magazine)
            charToCount[character] = charToCount.GetValueOrDefault(character, 0) + 1;

        foreach (char character in ransomNote)
        {
            charToCount[character] = charToCount.GetValueOrDefault(character, 0) - 1;

            if (charToCount[character] < 0)
            {
                return false;
            }
        }

        return true;
    }
}
⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

def can_construct(ransom_note, magazine)
  char_to_count = Hash.new(0)

  magazine.each_char { |c| char_to_count[c] += 1 }

  ransom_note.each_char do |c|
    char_to_count[c] -= 1
    return false if char_to_count[c] < 0
  end

  true
end
⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

func canConstruct(ransomNote string, magazine string) bool {
    charToCount := make(map[rune]int)

    for _, char := range magazine {
        charToCount[char]++
    }

    for _, char := range ransomNote {
        charToCount[char]--

        if charToCount[char] < 0 {
            return false
        }
    }

    return true
}
⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:
  1. This question is equivalent to asking whether magazine can contain all the characters in ransomNote.

  2. First count magazine to get the number of words corresponding to each character, and store the result in Map. Each time is an addition one operation.

  3. What to do next?

    Click to view the answer

    Traverses ransomNote and subtracts one from the number corresponding to the current character (reverse operation). If the number of a character is less than 0, return false.

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char, int> char_to_count;

        for (char character : magazine) {
            char_to_count[character]++;
        }

        for (char character : ransomNote) {
            char_to_count[character]--;

            if (char_to_count[character] < 0) {
                return false;
            }
        }

        return true;
    }
};
⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #383 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.