Java [Leetcode 383]Ransom Note

题目描述:

Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?it ?will ?return ?false. ??

Each ?letter? in? the? magazine ?string ?can? only ?be? used ?once? in? your ?ransom? note.

Note:
You may assume that both strings contain only lowercase letters.

canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true

解题思路:

开26个数组存入magzine字母的数目,每个位置存对应字母的数目;

对ransom字符串来说,每读一个字母,则将对应位置的字母数目减一,如果某个字母数目小于0了,则表明字母不够用,从而返回false;否则返回true。

代码如下:

public class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
    	int[] ran_array = new int[26];
    	for(int i = 0; i < magazine.length(); i++){
    		ran_array[magazine.charAt(i) - ‘a‘]++;
    	}

    	for(int i = 0; i < ransomNote.length(); i++){
    		if(--ran_array[ransomNote.charAt(i) - ‘a‘] < 0)
    			return false;
    	}

    	return true;

    }
}

  

时间: 2024-10-23 21:26:10

Java [Leetcode 383]Ransom Note的相关文章

Leetcode 383. Ransom Note JAVA语言

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the 

LeetCode 383 Ransom Note

Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?it ?will

383. Ransom Note【easy】

383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return fal

&lt;LeetCode OJ&gt; 383. Ransom Note

Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?it ?will

[LeetCode]383. Ransom Note 解题小结

题目: ?Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?it

leetcode修炼之路——383. Ransom Note

题目是这样的 Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?i

[LeetCode] NO.383 Ransom Note

[题目] Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?constructed ?from ?the ?magazines ; ?otherwise, ?it

【Leetcode】Ransom Note

题目链接:https://leetcode.com/problems/ransom-note/ 题目: Given? an ?arbitrary? ransom? note? string ?and ?another ?string ?containing ?letters from? all ?the ?magazines,? write ?a ?function ?that ?will ?return ?true ?if ?the ?ransom ? note ?can ?be ?const

383. Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each letter in the