当前位置:网站首页>Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)
Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)
2022-06-23 03:47:00 【我不是萧海哇~~~~】

给你两个长度相同的字符串,s 和 t。
将 s 中的第 i 个字符变到 t 中的第 i 个字符需要 |s[i] - t[i]| 的开销(开销可能为 0),也就是两个字符的 ASCII 码值的差的绝对值。
用于变更字符串的最大预算是 maxCost。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
如果你可以将 s 的子字符串转化为它在 t 中对应的子字符串,则返回可以转化的最大长度。
如果 s 中没有子字符串可以转化成 t 中对应的子字符串,则返回 0。
示例 1:
输入:s = "abcd", t = "bcdf", maxCost = 3
输出:3
解释:s 中的 "abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3。
示例 2:
输入:s = "abcd", t = "cdef", maxCost = 3
输出:1
解释:s 中的任一字符要想变成 t 中对应的字符,其开销都是 2。因此,最大长度为 1。
示例 3:
输入:s = "abcd", t = "acde", maxCost = 0
输出:1
解释:a -> a, cost = 0,字符串未发生变化,所以最大长度为 1。
提示:
- 1 <= s.length, t.length <= 10^5
- 0 <= maxCost <= 10^6
- s 和 t 都只含小写英文字母。
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;
}
};
边栏推荐
- JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
- leetcode 91. Decode Ways 解码方法(中等)
- There is a problem with redis startup
- 虫子 STM32 高级定时器 (哈哈我说实话硬件定时器不能体现实力,实际上想把内核定时器发上来的,一想算了,慢慢来吧)
- leetcode 91. Decode ways (medium)
- Bug STM32 interrupt (everyone knows)
- 怎样能在小程序中实现视频通话及互动直播功能?
- 粒子动画背景登录页面particles.js
- Introduction to deep learning
- Similar to RZ / SZ, trzsz supporting TMUX has released a new version
猜你喜欢
随机推荐
mysql存储引擎之Myisam和Innodb的区别
Imitation 360 desktop suspended ball plug-in
Pytoch --- use pytoch's pre training model to realize four weather classification problems
How to process large volume xlsx/csv/txt files?
Mobile terminal city list sorting JS plug-in vertitylist js
What is the digital "true" twin? At last someone made it clear!
深度学习 TensorFlow入门
QMainWindow
虫子 日期类 上 太子语言
移动端城市列表排序js插件vercitylist.js
How e-commerce makes use of small programs
How to solve the problem that the web page fails to log in after the easycvr service is started?
Preface
leetcode 91. Decode Ways 解码方法(中等)
自媒体时代的贤内助——AI 视频云
在线文本过滤小于指定长度工具
mysql优化,sql执行非常卡顿,不改变sql结构达到10秒内结束
linux下的开源数据库是什么
华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
仿360桌面悬浮球插件








