Leetcode #383 Ransom Note

Leetcode #383 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.

?

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.

We can Solve this easily with an hash map in python:

We all know that Counter(array) in Python gives a hash map with the key and value as the count of that particular key.

Follow these steps:

  1. Define mag_counter array as a counter hash map
  2. loop through each char in ransomNote
  3. if the count is equal to 0 then its equivalent to not present, return False
  4. else if it is found decrement the count by 1 everytime it is found in mag_counter
  5. return True

Code snippet

Shubham Mishra

ServiceNow Developer @RSC Solution | ServiceNow Developer/Full Stack Developer | Project Manager | React.js | Python | SQL | Mongo DB | Master’s in Computer Science

1 年

Amazing idea to solve RansomeNote problem using python. Keep it up Prathima Seethalam Radhakrishna . ??

回复

要查看或添加评论,请登录

Prathima Seethalam Radhakrishna的更多文章

  • Leetcode Problem 338. Counting bits (Dynamic Programming)

    Leetcode Problem 338. Counting bits (Dynamic Programming)

    Given an integer , return an array of length such that for each (), is the number of 's in the binary representation of…

    1 条评论
  • Optimize shuffle an array (Leetcode 384 MEDIUM)

    Optimize shuffle an array (Leetcode 384 MEDIUM)

    Let's Optimize Shuffle an array Problem - Leetcode 384. Shuffle an Array We know we can directly use random.

    1 条评论
  • Simple React App Creation

    Simple React App Creation

    As a beginner let’s learn the development of a new React Project from scratch: Prerequisites: 1. Install Node from:…

    1 条评论

社区洞察

其他会员也浏览了