当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

背景彩带动画插件ribbon.js

How to use MySQL index well

Getting started with tensorflow

How does flutter achieve different zoom animation effects

8 key indicators to measure technology debt in 2022

How to use shell script to monitor file changes

A summary of PostgreSQL data types. All the people are here

Background ribbon animation plug-in ribbon js

【Pytorch】用自动微分求sin(x)的导数

flutter系列之:flutter中的Wrap
随机推荐
Does the network disk also roll inside?
How to make the page number start from the specified page in word
【Pytorch】用自动微分求sin(x)的导数
Pta:7-85 data spacing (overload + function template)
Svg+js smart home monitoring grid layout
How node+express operates cookies
mysql如何删除表的一行数据
Monitoring artifact ZABBIX, from deployment to application, goes deep layer by layer
[learn FPGA programming from scratch -40]: Advanced - Design - competition and risk
Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly
PTA:7-64 该日是该年的第几天
Imitation 360 desktop suspended ball plug-in
12 excellent practices of wireless network security
炫酷鼠标跟随动画js插件5种
Introduction and use of MySQL view
Bug STM32 interrupt (everyone knows)
QMainWindow
How MySQL deletes a row of data in a table
PTA:7-63 计算高考状元
Form development mode