LeetCode: Ransom Note
1 public class Solution { 2 public boolean canConstruct(String ransomNote, String magazine) { 3 int[] ransomNum = new int[256]; 4 int[] magNum = new int[256]; 5 for (int i = 0; i < 256; i++) { 6 ransomNum[i] = magNum[i] = 0; 7 } 8 for (int i = 0; i < ransomNote.length(); i++) { 9 ransomNum[(int)ransomNote.charAt(i)]++; 10 } 11 for (int i = 0; i < magazine.length(); i++) { 12 magNum[(int)magazine.charAt(i)]++; 13 } 14 for (int i = 0; i < 256; i++) { 15 if (ransomNum[i] > magNum[i]) return false; 16 } 17 return true; 18 } 19 }