304 North Cardinal St.
Dorchester Center, MA 02124

# Ransom Note LeetCode Solution

## Problem – Ransom Note LeetCode Solution

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.

### Ransom Note LeetCode Solution in Java

``````public class Solution {
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;
}
}
``````

### Ransom Note LeetCode Solution in Python

``````def canConstruct(self, ransomNote, magazine):
return not collections.Counter(ransomNote) - collections.Counter(magazine)
``````

### Ransom Note LeetCode Solution in C++

``````bool canConstruct(string ransomNote, string magazine) {

int count[26] = {0};
for(char ch : magazine)
count[ch - 'a']++;

for(char ch : ransomNote)
if(count[ch - 'a']-- <= 0)
return false;

return true;
}
``````
##### Ransom Note LeetCode Solution Review:

In our experience, we suggest you solve this Ransom Note LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Ransom Note LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Ransom Note LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions