Question

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/ransom-note/
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
Ideas
1、Answer( Java ) - 字符统计
解法思路:字符统计
Code
/**
* @author Listen 1024
* @description 383. 赎金信( 字符统计 )
* 时间复杂度 O(m+n) m 及 n 分别是两个字符串的长度
* 空间复杂度 O(∣S∣) S 是字符集,这道题中 S 为全部小写英语字母,因此 |S| = 26
* @date 2022-05-14 9:00
*/
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if (ransomNote.length() > magazine.length()) {
return false;
}
int[] cnt = new int[26];
for (char res : magazine.toCharArray()) {
cnt[res - 'a']++;
}
for (char res : ransomNote.toCharArray()) {
cnt[res - 'a']--;
if (cnt[res - 'a'] < 0) {
return false;
}
}
return true;
}
}
//部分题解参考链接(如侵删)
https://leetcode.cn/problems/ransom-note/solution/shu-jin-xin-by-leetcode-solution-ji8a/

被折叠的 条评论
为什么被折叠?



