This article is another part of a series on leet code problems solutions, and it provides a solution to the leet code problem number 383. This is a Leet code problem named Ransom Note We will solve it using python and it is the best space and time-optimized solution.
Question of Ransom Note
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
.
Write a function that returns true if the ransom note can be created from the magazines; otherwise, it returns false. The function will be given an arbitrary ransom note string and another string containing letters from all the publications.
The condition is that you can only use each letter once in the magazine string once in your ransom message as given.
Example
Example 1
Input: ransomNote = "a", magazine = "b"
Output: false
Example 2
Input: ransomNote = "aa", magazine = "ab"
Output: false
Constraints
1 <= ransomNote.length, magazine.length <= 105
ransomNote
andmagazine
consist of lowercase English letters.
Approach to solve Ransom Note
To solve this problem we will iterate each character of ransomNote and search that character in magazine. If the character is found in magazine then we delete the first found character. If the character is not found in magazine than we return false because after this we cannot create ransomNote from magazine.
Algorithm
This method performs the following steps for a given input array:
- Check for base condition. check if ransomNote is grater than magazine if true return false.
- Start for loop for each letter of ransomNote.
- Check if letter is in magazine or not.
- if found then then delete that character of magazine.
- If not found return false.
- End if statement
- End for loop
- In end, if every character of magazine is found in ransomNote then return True.
Python Code for Ransom Note
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
#check if ransomNote is grater than magazine if true return false
if(len(ransomNote) > len(magazine)):
return False
else:
# search each letter of ransomNote in magzine
for letter in ransomNote:
if letter in magazine:
#if found then then delete that character of magazine
magazine = magazine.replace(letter, '', 1)
else:
#otherwise return false
return False
return True
Complexity Analysis
Time Complexity: O(n)
Where n in size of ransomNote
Space Complexity: O(1)
Related Posts:
- Find Pivot Index (Leet Code-724)
- Spiral Matrix Problem Solution
- Isomorphic Strings
- Two Sum IV - Input is a BST (Leet Code-653)
- Search in a Binary Search Tree (Leet Code-700)
- Invert Binary Tree (Leet Code-266)
- Symmetric Tree (Leet Code-101)
- Maximum Depth of Binary Tree (Leet Code-104)
- Binary Tree Postorder Traversal (Leet Code-145)
- Binary Tree Inorder Traversal (Leet Code - 94)
- Binary Tree Pre-Order Traversal
- Binary Tree Level Order Traversal (Leet Code-102)
- Valid Parentheses (Leet Code-20)
- Remove Duplicates from Sorted List (Leet Code-83)
- Reverse Linked List (Leet code -206)
- Remove Linked List Elements (Leet Code-203)
- Merge Two Sorted Lists (Leet code-21)
- First Unique Character in a String (Leet code-387)
- Valid Sudoku (Leet Code-36)
- Search a 2D Matrix (Leet Code-74)
- Linked List Cycle (Leet Code-141)
- Pascal's Triangle (Leet Code - 118)
- Reshape the Matrix (Leet Code-566)
- Best Time to Buy and Sell Stock (Leet code-121)
- The Intersection of Two Arrays II (Leet Code)
- Binary Search - Leet Code
- Two Sum - Leet Code