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 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

解法一:

 class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char, int> m_rans; for (int i = ; i < ransomNote.length(); ++i) {
++m_rans[ransomNote[i]];
} for (int i = ; i < magazine.length(); ++i) {
if (m_rans.find(magazine[i]) != m_rans.end()) {
--m_rans[magazine[i]];
}
} for (map<char, int>::iterator it = m_rans.begin(); it != m_rans.end(); ++it) {
if (it->second > ) {
return false;
}
} return true;
}
};

解法二:

 public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] arr = new int[];
for (int i = ; i < magazine.length(); i++) {
arr[magazine.charAt(i) - 'a']++;
}
for (int i = ; i < ransomNote.length(); i++) {
if(--arr[ransomNote.charAt(i)-'a'] < ) {
return false;
}
}
return true;
}
}

参考@yidongwang 的代码。

解法三:

 class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char, int> map();
for (int i = ; i < magazine.size(); ++i)
++map[magazine[i]];
for (int j = ; j < ransomNote.size(); ++j)
if (--map[ransomNote[j]] < )
return false;
return true;
}
};

参考@haruhiku 的代码

解法四:

 class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> vec(, );
for (int i = ; i < magazine.size(); ++i)
++vec[magazine[i] - 'a'];
for (int j = ; j < ransomNote.size(); ++j)
if (--vec[ransomNote[j] - 'a'] < )
return false;
return true;
}
};

参考@haruhiku 的代码

383. Ransom Note【easy】的更多相关文章

  1. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  2. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  3. 661. Image Smoother【easy】

    661. Image Smoother[easy] Given a 2D integer matrix M representing the gray scale of an image, you n ...

  4. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 605. Can Place Flowers【easy】

    605. Can Place Flowers[easy] Suppose you have a long flowerbed in which some of the plots are plante ...

  7. 485. Max Consecutive Ones【easy】

    485. Max Consecutive Ones[easy] Given a binary array, find the maximum number of consecutive 1s in t ...

  8. 189. Rotate Array【easy】

    189. Rotate Array[easy] Rotate an array of n elements to the right by k steps. For example, with n = ...

  9. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

随机推荐

  1. Largest Divisible Subset -- LeetCode

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  2. luogu P3368 【模板】树状数组 2

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数数加上x 2.求出某一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别表示该数列数字的个数和操作的总个数. ...

  3. [Codeforces 7E] Defining Macros

    Link:http://codeforces.com/problemset/problem/7/E Brief Introduction:一个表达式由多个“Macros”组成,每个Macro都为一个整 ...

  4. 【找规律】URAL - 2069 - Hard Rock

    题解及证明:http://www.cnblogs.com/StupidBoy/p/5241258.html #include<cstdio> #include<algorithm&g ...

  5. “自适应”高度的 textarea 文本输入框

    写在前面 那啥,在我的那个很安静的一个 CSS 群(群号:82991297)突然看到有人在问一个问题. 使用 css 如何实现:textarea 如何实现高度自适应? 当时看到这个问题的时候,我脑中只 ...

  6. 【spring data jpa】使用jpa进行update更新操作,避免空字段和更新后即时返回给前台的问题

    修改操作  如果使用的JPA,应该是 findByUid之后 将新的值对old进行赋值,最后对old值进行flush(),即可完成更新,而不应该去更新新的实体. 示例代码: 以后补充

  7. 集合—ArrayList

    ArrayList也叫作数组列表 public static void main(String[] args) { List list1 = new ArrayList<String>() ...

  8. 利用forever在Linux上实现Node.js项目自启动

    在一台计算机上手动跑Node项目简单,node xx.js就搞定了,想让Node项目后台执行,尽管不能直接用node命令搞定,可是在安装了forever这个包以后.还是非常轻松的.只是要是在远程ser ...

  9. PuTTY连接Linuxserver常常断线解决方式

    PuTTY在远程连接server之后.常常会断线提示"Software caused connection abort",并且常常在非常短的时间内就失去连接. 解决方式例如以下: ...

  10. 如何注册ocx文件

    32位系统: 将文件放到c:\windows\system目录注册 运行:Regsvr32 c:\windows\system\xxx.ocx取消注册运行:Regsvr32.exe /u c:\win ...