当前位置:网站首页>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;
}
};
边栏推荐
- Online JSON to CSharp (c) class tool
- Similar to RZ / SZ, trzsz supporting TMUX has released a new version
- MySQL optimization, the SQL execution is very stuck, and the SQL structure will not be changed until it ends in 10 seconds
- AI video cloud: a good wife in the era of we media
- 电商如何借助小程序发力
- 华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
- PTA:7-65 饮料的价格
- 怎么用好MySQL索引
- 12 excellent practices of wireless network security
- Background ribbon animation plug-in ribbon js
猜你喜欢

How to ensure application security

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
![[Shangshui Shuo series] day three - preview4](/img/c1/e840304a0a32c283c8720315a56716.png)
[Shangshui Shuo series] day three - preview4

Latest programming language rankings

怎么使用Shell脚本实现监测文件变化

Twitter cooperates with Shopify to introduce merchant products into twitter shopping

Background ribbon animation plug-in ribbon js

redis 精讲系列介绍八 - 淘汰策略

QMainWindow

Compilation, installation and global configuration section description of haproxy
随机推荐
京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”
高效的远程办公经验 | 社区征文
如何保证应用程序的安全性
Dynamics 365 插件中权限操作
APM 工具 SkyWalking 是什么
What is the digital "true" twin? At last someone made it clear!
Prince language under insect date category
在线文本过滤小于指定长度工具
PTA:7-85 数据的间距问题(重载+函数模板)
bubble sort
【二叉樹進階】AVLTree - 平衡二叉搜索樹
P1363 幻象迷宫(dfs)
Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly
怎么用好MySQL索引
在 KubeSphere 上部署 Apache Pulsar
leetcode 91. Decode Ways 解码方法(中等)
PTA: Simulation Implementation of 7-86 set (function template)
仿360桌面悬浮球插件
基于FPGA的VGA协议实现
Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!