当前位置:网站首页>Leetcode 1208. Try to make the strings equal as much as possible (finally solved, good night)
Leetcode 1208. Try to make the strings equal as much as possible (finally solved, good night)
2022-06-23 04:37:00 【I'm not xiaohaiwa~~~~】

Give you two strings of the same length ,s and t.
take s No i Four characters change to t No i Two characters need to be |s[i] - t[i]| The cost of ( The cost may be 0), That's two characters ASCII The absolute value of the difference between code values .
The maximum budget for changing strings is maxCost. When converting strings , The total cost should be less than or equal to the budget , This also means that string conversion may be incomplete .
If you can put s The substring of is converted to it in t The substring corresponding to , Then returns the maximum length that can be converted .
If s There are no substrings that can be converted to t The substring corresponding to , Then return to 0.
Example 1:
Input :s = "abcd", t = "bcdf", maxCost = 3
Output :3
explain :s Medium "abc" It can be "bcd". The cost is 3, So the maximum length is 3.
Example 2:
Input :s = "abcd", t = "cdef", maxCost = 3
Output :1
explain :s If you want any character in to become t The corresponding characters in , The cost is 2. therefore , Maximum length is 1.
Example 3:
Input :s = "abcd", t = "acde", maxCost = 0
Output :1
explain :a -> a, cost = 0, The string has not changed , So the maximum length is 1.
Tips :
- 1 <= s.length, t.length <= 10^5
- 0 <= maxCost <= 10^6
- s and t It's all lowercase letters .
Code:
class Solution {
public:
int equalSubstring(string s, string t, int maxCost) {
vector<int>vec;
for(int i=0;i<s.length();i++)
{
vec.push_back(abs(s[i]-t[i]));
}
int cnt=0;
int sum=0;
int res=0;
bool flag=false;
queue<int>q;
for(int i=0;i<vec.size();i++)
{
sum+=vec[i];
q.push(vec[i]);
if(sum<=maxCost)
{
cnt=max((int)q.size(),cnt);
}
else
{
sum-=q.front();
q.pop();
cnt=max((int)q.size(),cnt);
}
}
// cout<<cnt<<"+"<<endl;
return cnt;
}
};
边栏推荐
- 怎么使用Shell脚本实现监测文件变化
- Introduction and use of MySQL view
- P1347 排序(topo)
- mysql能不能在linux中使用
- Review the SQL row column conversion, and the performance has been improved
- [从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
- Can MySQL be used in Linux
- 城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!
- PTA:7-61 师生信息管理
- Does the network disk also roll inside?
猜你喜欢

移动端城市列表排序js插件vercitylist.js

Idea import module

How to ensure application security

Twitter cooperates with Shopify to introduce merchant products into twitter shopping

8 key indicators to measure technology debt in 2022

Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors

Svg+js smart home monitoring grid layout

Pytorch---Pytorch进行自定义Dataset

Halcon胶线检测—模板匹配、位姿变换、胶宽,胶连续性检测

最新编程语言排行榜
随机推荐
P1363 phantom maze (DFS)
Flutter series: wrap in flutter
给你的AppImage创建桌面快捷方式
[pycharm] ide Eval resetter
mysql能不能在linux中使用
背景彩带动画插件ribbon.js
什么是元数据
粒子动画背景登录页面particles.js
PTA:7-69 数据的间距问题
PTA:7-60 宠物的生长
How to make the page number start from the specified page in word
Particle animation background login page particles js
PTA:7-67 友元很简单2016final
How MySQL deletes a row of data in a table
浅析2022年物联网现状
PTA:7-85 数据的间距问题(重载+函数模板)
Analysis on the current situation of the Internet of things in 2022
2022年烷基化工艺考题及模拟考试
Dynamics 365 插件中权限操作
语料库数据处理个案实例(词性赋码、词性还原)