Logo

dev-resources.site

for different kinds of informations.

LeetCode Challenge: 383. Ransom Note - JavaScript Solution πŸš€

Published at
1/13/2025
Categories
javascript
programming
leetcode
interview
Author
rahulgithubweb
Author
14 person written this
rahulgithubweb
open
LeetCode Challenge: 383. Ransom Note - JavaScript Solution πŸš€

Top Interview 150

The Ransom Note problem is a simple string manipulation challenge that tests your ability to manage character counts efficiently. Let’s solve LeetCode 383 step by step.


πŸš€ Problem Description

Given two strings ransomNote and magazine:

  • Return true if ransomNote can be constructed using letters from magazine.
  • Each letter in magazine can only be used once in ransomNote.

πŸ’‘ Examples

Example 1

Input: ransomNote = "a", magazine = "b"  
Output: false
Enter fullscreen mode Exit fullscreen mode

Example 2

Input: ransomNote = "aa", magazine = "ab"  
Output: false
Enter fullscreen mode Exit fullscreen mode

Example 3

Input: ransomNote = "aa", magazine = "aab"  
Output: true
Enter fullscreen mode Exit fullscreen mode

πŸ† JavaScript Solution

We solve this problem by counting the occurrences of each letter in magazine and comparing them with the required counts for ransomNote.


Implementation

var canConstruct = function(ransomNote, magazine) {
    const charCount = {};

    for (let char of magazine) {
        charCount[char] = (charCount[char] || 0) + 1;
    }

    for (let char of ransomNote) {
        if (!charCount[char] || charCount[char] <= 0) {
            return false;
        }
        charCount[char]--;
    }

    return true;
};
Enter fullscreen mode Exit fullscreen mode

πŸ” How It Works

  1. Count Characters in Magazine:

    • Create a hash map (charCount) to store the frequency of each character in magazine.
  2. Validate Against Ransom Note:

    • Iterate through each character in ransomNote.
    • Check if the character is available in charCount.
    • If not, return false.
    • Decrement the count of the character in charCount.
  3. Return Result:

    • If all characters are found with sufficient counts, return true.

πŸ”‘ Complexity Analysis

  • Time Complexity: O(n+m), where n is the length of magazine and m is the length of ransomNote.

    • Counting characters in magazine takes O(n).
    • Validating ransomNote takes O(m).
  • Space Complexity: O(k), where k is the number of unique characters in magazine.


πŸ“‹ Dry Run

Input: ransomNote = "aa", magazine = "aab"
Dry RUn

Output: true


✨ Pro Tips for Interviews

  1. Clarify Constraints:

    • Ensure that ransomNote and magazine only contain lowercase letters.
    • Ask about edge cases, such as empty strings.
  2. Discuss Optimizations:

    • Highlight how using a hash map ensures efficient character counting.
  3. Edge Cases:

    • ransomNote longer than magazine β†’ immediately return false.
    • All characters in magazine but insufficient counts.

πŸ“š Learn More

Check out the full explanation and code walkthrough on my previous Dev.to post:
πŸ‘‰ Set Matrix Zeroes - JavaScript Solution

What’s your preferred method to solve this problem? Let’s discuss! πŸš€


Study

leetcode Article's
30 articles in total
Favicon
Neetcode Roadmap Part 1
Favicon
2429. Minimize XOR
Favicon
A tΓ©cnica dos dois ponteiros
Favicon
2657. Find the Prefix Common Array of Two Arrays
Favicon
Time Complexity, Big-O for Beginners
Favicon
LeetCode Challenge: 383. Ransom Note - JavaScript Solution πŸš€
Favicon
3223. Minimum Length of String After Operations
Favicon
Leet code
Favicon
2116. Check if a Parentheses String Can Be Valid
Favicon
LeetCode Challenge: 73. Set Matrix Zeroes - JavaScript Solution πŸš€
Favicon
LeetCode Challenge: 290. Word Pattern - JavaScript Solution πŸš€
Favicon
LeetCode Challenge: 205. Isomorphic Strings - JavaScript Solution πŸš€
Favicon
Leetcode: 73 Set Matrix Zeroes
Favicon
LeetCode Challenge: 36.Valid Sudoku - JavaScript Solution πŸš€
Favicon
Count prefix and suffix I and II
Favicon
Leetcode Blind 75
Favicon
Rabin Karp (hashing) String pattern matching
Favicon
Leetcode β€” 2942. Find Words Containing Character
Favicon
Automating Your LeetCode Journey: Building an Enterprise-Grade LeetCode to GitHub Sync System
Favicon
Understanding the XOR Operator: A Powerful Tool in Computing
Favicon
Kadane's Algorithm: Leetcode 53 Maximum subarray
Favicon
1768. Merge Strings Alternately
Favicon
Find all anagrams in the string[Fixed Window pattern]
Favicon
No of ways to split Array
Favicon
Leetcode β€” 3289. The Two Sneaky Numbers of Digitville
Favicon
Range sum query 2D - Immutable
Favicon
Range Sum Query - Immutable
Favicon
Count vowel strings in ranges
Favicon
Yay! Reached 1035+ days Daily Coding Streak on Leetcode!
Favicon
Leetcode 75. Sort Colors

Featured ones: