| 7 | */ |
| 8 | class Solution { |
| 9 | public boolean canConstruct(String ransomNote, String magazine) { |
| 10 | |
| 11 | if (ransomNote.length() > magazine.length()) |
| 12 | return false; |
| 13 | |
| 14 | /* Logic : character count in ransomNote should be equal to OR less than magazine character count */ |
| 15 | |
| 16 | int [] ransomNoteChCount = new int[26]; // lower case characters are only 26 |
| 17 | |
| 18 | for (int i=0;i<ransomNote.length();i++){ |
| 19 | ransomNoteChCount[ransomNote.charAt(i) - 'a']++; |
| 20 | } |
| 21 | |
| 22 | for (int i=0;i<magazine.length();i++){ |
| 23 | ransomNoteChCount[magazine.charAt(i) - 'a']--; |
| 24 | } |
| 25 | |
| 26 | for(int i=0;i<26;i++){ |
| 27 | if (ransomNoteChCount[i] > 0){ |
| 28 | return false; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | } |