Leetcode 383 Ransom Note

lc383 Ransom Note

两个for

第一个记录sourse字符串每种字母出现次数

第二个看现有字母是否能够填满target

 1 class Solution {
 2     public boolean canConstruct(String ransomNote, String magazine) {
 3         if(ransomNote.length() == 0)
 4             return true;
 5 
 6         int countM[] = new int[26];
 7         //Arrays.fill(countM, 1);
 8         
 9         for(int i=0; i<magazine.length(); i++)
10             countM[magazine.charAt(i)-'a']++;
11         for(int i=0; i<ransomNote.length(); i++){
12             countM[ransomNote.charAt(i)-'a']--;
13             if(countM[ransomNote.charAt(i)-'a'] == -1)
14                 return false;
15         }
16         
17         return true;
18         
19     }
20 }
原文地址:https://www.cnblogs.com/hwd9654/p/10941830.html