Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • 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
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Ransom Note problem solution

YASH PAL, 31 July 2024

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

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

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

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

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes