g0301_0400.s0383_ransom_note.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0301_0400.s0383_ransom_note;
// #Easy #String #Hash_Table #Counting
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] a = new int[26];
int n = ransomNote.length();
for (int i = 0; i < n; i++) {
a[ransomNote.charAt(i) - 97]++;
}
for (int i = 0; i < magazine.length() && n != 0; i++) {
if (a[magazine.charAt(i) - 97] > 0) {
n--;
a[magazine.charAt(i) - 97]--;
}
}
return n == 0;
}
}