Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Leetcode Ransom Note problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Ransom Note problem solution you have given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise. Each letter in magazine can only be used once in ransomNote.

Leetcode Ransom Note problem solution

Leetcode Ransom Note problem solution in Python.

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        note,mag = Counter(ransomNote), Counter(magazine)
        if note & mag == note: return True
        return False

Ransom Note problem solution in Java.

class Solution {
    public boolean canConstruct(String ransomeNote, String magazine) {
        HashMap<Character,Integer> hashmap = new HashMap<>();
        for(int i=0;i<magazine.length();i++)
        {
            if(!hashmap.containsKey(magazine.charAt(i)))
                hashmap.put(magazine.charAt(i),1);
            else
                hashmap.put(magazine.charAt(i),hashmap.get(magazine.charAt(i))+1);
        }
        
        int start=0;
        while(start<ransomeNote.length())
        {
            if(!hashmap.containsKey(ransomeNote.charAt(start)))
                return false;
            else
            {
                hashmap.put(ransomeNote.charAt(start),hashmap.get(ransomeNote.charAt(start))-1);
                if(hashmap.get(ransomeNote.charAt(start))==0)
                    hashmap.remove(ransomeNote.charAt(start));
            }
            
            start++;
        }
        return true;
    }
}

Problem solution in C++.

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        int r[26] = {0};
        int m[26] = {0};
        
        for(auto c : ransomNote)    r[c - 'a']++;
        for(auto c : magazine)      m[c - 'a']++;
        for(int i = 0; i < 26; i++) if(r[i] > m[i]) return false;
        return true;
    }
};

Problem solution in C.

bool canConstruct(char * ransomNote, char * magazine){

int t[26] = {0};

for(char *str = magazine; *str; str++ ) {
    if(*str >= 97 && *str <= 122) {
        t[*str - 97] += 1;
    }
}

for(char *str = ransomNote; *str; str++) {
    if(t[*str -97] == 0)
        return false;
    else if (t[*str -97] > 0)
        t[*str -97] -= 1;
}

return true;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for just 7 dollars. Ask all your career-related questions to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes