https://leetcode.com/problems/ransom-note/ public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
char[] chRan = ransomNote.toCharArray();
char[] chMag = magazine.toCharArray();
Arrays.sort(chRan);
Arrays.sort(chMag);
int j = 0;
for (int i=0; i<chRan.length; i++) {
for (; j<chMag.length; j++) {
if (chMag[j] > chRan[i]) {
return false;
}
if (chMag[j] == chRan[i]) {
break;
}
}
if (j < chMag.length) {
j++;
}
else {
return false;
}
}
return true;
}
}
相关文章
- 04-09ransom-note