【力扣】赎金信

https://leetcode.cn/problems/ransom-note/submissions/

 1 class Solution {
 2     public boolean canConstruct(String ransomNote, String magazine) {
 3         // 两个字符串长度不一样,则返回true
 4         if(ransomNote.length() > magazine.length()){
 5             return false;
 6         }
 7         int[] cnt = new int[26];
 8         for (char c : magazine.toCharArray()) {
 9             cnt[c - 'a']++;
10         }
11         for (char c : ransomNote.toCharArray()) {
12             cnt[c - 'a']--;
13             if(cnt[c - 'a'] < 0) {
14                 return false;
15             }
16         }
17         return true;
18     }
19 }

 

posted @ 2023-08-25 19:04  陆陆无为而治者  阅读(3)  评论(0编辑  收藏  举报