当前位置:网站首页>345 leetcode ransom letter
345 leetcode ransom letter
2022-04-22 21:08:00 【sp_ thirteen billion two hundred and thirty million four hundre】

If ransomNote Is longer than magazine longer , That must be wrong
First traversal magazine, Count the number of occurrences of each character , Re traversal ransomNote, Reduce the number of corresponding characters in the array by one at a time , If the value after subtracting one is less than 0 了 , This character is no longer in the array , So it's wrong
class Solution
{
public:
bool canConstruct(string ransomNote, string magazine)
{
int len1 = ransomNote.size();
int len2 = magazine.size();
if (len1 > len2) return false;
vector<int>ve(26,0);
for (auto& ch : magazine)
{
++ve[ch - 'a'];
}
for (auto& ch : ransomNote)
{
if (--ve[ch - 'a'] < 0)
{
return false;
}
}
return true;
}
};
int main()
{
Solution A;
cout << A.canConstruct("bg","bbg") << endl;
return 0;
}
Time complexity :O(m+n), among m Is string ransomNote The length of ,n Is string magazine The length of , We only need to traverse two characters once
Spatial complexity :O(∣S∣),S It's a character set. , In this question S All lowercase English letters , therefore |S| = 26
版权声明
本文为[sp_ thirteen billion two hundred and thirty million four hundre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222100466335.html
边栏推荐
- Use of dSPACE simulation platform
- Smart agriculture has become a development path, give full play to intelligence and liberate manpower
- 工控安全解决方案
- SEREDS解串模块简介以及硬件实现
- 2022 G3 boiler water treatment national question bank and online simulation examination
- LeetCode刷题日记 - 剑指 Offer II 070. 排序数组中只出现一次的数字
- DSPACE模拟简单事故现场
- kubernetes_ How to solve the problem that namespace cannot be deleted
- Solution Sudoku [pre DS hash structure + component DFS | pre-hash-1-9 feature -- binary state compression + DFS]
- OpenVX-将Image文件[pgm格式]读写为vx_image对象,以及写操作
猜你喜欢

毕业五年,如何从月薪5K涨到年薪50W+,需要掌握哪些核心技能?

HDLBits(十)学习笔记——有限状态机(FSM1 - Lemmings4)

JMeter data and software

2、 Linear regression

QTP11使用教程

Application value of smart agriculture app software

Summary of software testing knowledge points | JMeter implementation Interface Association

Selenium_ Webdriver video automation script sharing

基于PAOGD的角色动画基础设计

88 r user portrait linear regression logical regression comprehensive practice 1
随机推荐
手写链表~内含单向链表和双向链表,请大家放心食用
浅学 “等待唤醒机制 ”
kubernetes_Namespace无法删除的解决方法
. 102 keyboard move div
Leetcode Hot 100
Return to ladder-l2-025 divide and rule (25 points)
站长工具大全-站长软件网站-站长工具网站-站长必备工具免费
Nodejs notes 3
[NOIP2012]借教室
2、 Linear regression
解数独[Pre-DS-hash结构 + component-DFS | Pre-hash-1-9特性--二进制状态压缩 + DFS]
2022 R2 mobile pressure vessel filling training test questions and online simulation test
(1) UART subsystem learning plan
基于GD32F1x0手动编程实现简易freertos之任务阻塞延时
LeetCode刷题日记 - 15. 三数之和
[play Lighthouse] build WooCommerce store, enable Alipay to pay in person.
Beyond compare enforces the use of binary transfer to ensure that the files are the same
Chapter I Introduction to virtual reality technology
344-Leetcode 二叉树的所有路径
博途PLC用户自定义数据类型