package com.zmz.algorithm.hashtable;

import java.util.HashMap;

/**
 * @author 张明泽
 * Create by 2022/5/31 11:45
 * 赎金信
 * LeetCode-383
 */
public class RansomLetter {
    public static void main(String[] args) {
        String s1 = "aa";
        String s2 = "ab";
        Boolean f = canConstruct(s1,s2);
        System.out.println(f);
    }
    public static Boolean canConstruct(String s1, String s2) {
        HashMap<Character,Integer> hashMap = new HashMap<>();
        for (char c : s2.toCharArray()) {
            if (hashMap.containsKey(c)) {
                hashMap.put(c,hashMap.get(c) + 1);
            } else {
                hashMap.put(c,1);
            }
        }
        for (char c : s1.toCharArray()) {
            hashMap.put(c,hashMap.getOrDefault(c,0) - 1);
            if (hashMap.get(c) < 0) return false;
        }
        return true;
    }
}
最后修改:2022 年 06 月 02 日 10 : 02 AM
赏杯咖啡喝 谢谢您~