383:赎金信
This commit is contained in:
parent
ad18c28448
commit
b92462a244
70
src/main/java/leetcode/editor/cn/RansomNote.java
Normal file
70
src/main/java/leetcode/editor/cn/RansomNote.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
//给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面
|
||||||
|
//的字符构成。如果可以构成,返回 true ;否则返回 false。
|
||||||
|
//
|
||||||
|
// (题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:ransomNote = "a", magazine = "b"
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:ransomNote = "aa", magazine = "ab"
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:ransomNote = "aa", magazine = "aab"
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 你可以假设两个字符串均只含有小写字母。
|
||||||
|
//
|
||||||
|
// Related Topics 哈希表 字符串 计数
|
||||||
|
// 👍 160 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//383:赎金信
|
||||||
|
class RansomNote {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new RansomNote().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean canConstruct(String ransomNote, String magazine) {
|
||||||
|
if (ransomNote.length() > magazine.length()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int[] arr = new int[26];
|
||||||
|
for (char ch : magazine.toCharArray()) {
|
||||||
|
arr[ch - 'a']++;
|
||||||
|
}
|
||||||
|
for (char ch : ransomNote.toCharArray()) {
|
||||||
|
if (arr[ch - 'a'] < 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
arr[ch - 'a']--;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
35
src/main/java/leetcode/editor/cn/RansomNote.md
Normal file
35
src/main/java/leetcode/editor/cn/RansomNote.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<p>给定一个赎金信 (<code>ransom</code>) 字符串和一个杂志(<code>magazine</code>)字符串,判断第一个字符串 <code>ransom</code> 能不能由第二个字符串 <code>magazines</code> 里面的字符构成。如果可以构成,返回 <code>true</code> ;否则返回 <code>false</code>。</p>
|
||||||
|
|
||||||
|
<p>(题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>ransomNote = "a", magazine = "b"
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>ransomNote = "aa", magazine = "ab"
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>ransomNote = "aa", magazine = "aab"
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>你可以假设两个字符串均只含有小写字母。</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>哈希表</li><li>字符串</li><li>计数</li></div></div>\n<div><li>👍 160</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user