LeetCode383_383. Ransom Letter

LeetCode383_383. Ransom Letter

1. Description

Given you two strings: ransomNote and magazine, judge whether ransomNote can be composed of characters in magazine.

Returns true if it can; otherwise returns false.

Each character in magazine can only be used once in ransomNote.

Example 1:

输入:ransomNote = "a", magazine = "b"
输出:false

Example 2:

输入:ransomNote = "aa", magazine = "ab"
输出:false

Example 3:

输入:ransomNote = "aa", magazine = "aab"
输出:true

hint:

1 <= ransomNote.length, magazine.length <= 105
ransomNote and magazine are composed of lowercase English letters

Two, solution

Method 1: The ransom letter is actually to judge whether the first string can be composed of the second string, and each character can only be used once.
Calculate all the elements and times in the first string, and calculate the second one in the same way, the first one exists in the second and is less than the number of times

    /*
    执行结果:通过
    执行用时:21 ms, 在所有 Java 提交中击败了7.53%的用户
    内存消耗:42 MB, 在所有 Java 提交中击败了28.73%的用户
    通过测试用例:128 / 128
     */
    public boolean canConstruct(String ransomNote, String magazine) {
    
    
        boolean res = true;
    	/*if ("".equals(ransomNote)) {
			res =true;
		}*/
        Map<Character, Integer> map1 = new HashMap<Character, Integer>();
        Map<Character, Integer> map2 = new HashMap<Character, Integer>();
        for (int i = 0; i < ransomNote.length(); i++) {
    
    
            map1.put(ransomNote.charAt(i), (map1.get(ransomNote.charAt(i)) != null ? map1.get(ransomNote.charAt(i)) + 1 : 1));
        }
        for (int i = 0; i < magazine.length(); i++) {
    
    
            map2.put(magazine.charAt(i), (map2.get(magazine.charAt(i)) != null ? map2.get(magazine.charAt(i)) + 1 : 1));
        }
        for (Character key : map1.keySet()) {
    
    
            if (!map2.containsKey(key) || (map2.get(key) < map1.get(key))) {
    
    //注意判断的时候,正难则反!!!
                res = false;
                break;
            }
        }
        System.out.println("map1" + map1);
        System.out.println("map2" + map2);
        return res;
    }

LeetCode 367. Valid Perfect Squares
LeetCode 371. Sum of Two Integers
LeetCode 383. Ransom Letter LeetCode
387. First Unique Character in a String
LeetCode 389. Find Differences
LeetCode 404. Sum of Left Leafs
LeetCode 412. Fizz Buzz
LeetCode 414. Third Largest Number
LeetCode 415. Adding Strings
LeetCode 434. Number of Words in a String



Disclaimer:
        The copyright of the topic belongs to the original author. The code and related statements in the article are written by myself based on my understanding. The relevant pictures in the article are screenshots from my own practice and pictures corresponding to related technologies. If you have any objections, please contact to delete them. grateful. Reprint please indicate the source, thank you.


By luoyepiaoxue2014

Station B: https://space.bilibili.com/1523287361 Click to open the link
Weibo: http://weibo.com/luoyepiaoxue2014 Click to open the link

Guess you like

Origin blog.csdn.net/luoyepiaoxue2014/article/details/129929259