Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Java Programs – LeetCode – Ransom Note – Solution 1

Posted on 03/21/2025 By admin

https://leetcode.com/problems/ransom-note/

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines; otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

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:

  • You may assume that both strings contain only lowercase letters.

Sometimes reading a problem statement confuses you a lot like above. In short, we need to find if all characters of the first string are present in another string in the same count. For example, if first string contains 2 ‘a’ characters then second string also contains 2 ‘a’ characters. The order of characters does not matter.

  1. We need to find if all characters of first string are present in second string. It means the length of second string must be equal to or more than the length of first string. This should be first check before we think of actual logic.
  2. Take first char of first string and check if second string contains that char or not. As the problem statement says that “Each letter in the magazine string can only be used once in your ransom note.” we need to remove the matched characters from second string.
  3. We should also put some edge cases like if a given string is null.

Logic is simple and let’s do it using Java programming language.

package Easy;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/*
Given an arbitrary ransom note string and another string containing letters from all the magazines, 
write a function that will return true if the ransom note can be constructed from the magazines ; 
otherwise, it will return false.

Each letter in the magazine string can only be used once in your ransom note.

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:

You may assume that both strings contain only lowercase letters.

https://leetcode.com/problems/ransom-note/
*/

public class RansomNote {

        public static boolean canConstruct(String ransomNote, String magazine) {
                // Take length of first string
                int l1 = ransomNote.length();
                // Take length of second string
                int l2 = magazine.length();
                // Check if length of first string is greater than length of second string
                if (l1 > l2)
                        // terminate execution as basic condition fails
                        return false;
                // Iterate char by char of first string
                char[] ransomNoteChars = ransomNote.toCharArray();
                for (char charOfransomNote : ransomNoteChars) {
                        // Check char is present in magazine string
                        if (magazine.contains(Character.toString(charOfransomNote)))
                                // If magazine contains char, replace that with empty string. 
                                // I am using replaceFirst method to replace only first occurrence. 
                                magazine = magazine.replaceFirst(Character.toString(charOfransomNote), "");
                        else
                                // If char is not present in magazine string then return false
                                return false;
                }
                // If iteration is completed for each char it means all chars of ransom note is present in 
                // magazine note. 
                return true;

        }

        public static void main(String[] args) {
                System.out.println(canConstruct("aa", "aab"));
                System.out.println(canConstruct("aba", "aab"));
                System.out.println(canConstruct("abc", "ab"));
                System.out.println(canConstruct("b", "a"));
                System.out.println(canConstruct("amod maha", "mdoamaa h"));

        }

}
true
true
false
false
true

You can clone above Git Repo from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Learn Selenium With Quiz – Basic Level 8 – CSS Selector
Next Post: Postman Tutorial Part 28- Building Workflow in Postman Using Collection Runner – New Feature of Postman

Related Posts

Make Selenium Easy – Page 16 Uncategorized
How To Launch Firefox Browser in Selenium WebDriver – Java Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 43 – Create Parameterized Request by Reading Data From JSON in Postman Uncategorized
June 2019 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark