当前位置:网站首页>Leetcode 1208. 尽可能使字符串相等
Leetcode 1208. 尽可能使字符串相等
2022-06-23 03:48: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 都只含小写英文字母。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/get-equal-substrings-within-budget
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
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;
for(int i=0;i<vec.size();i++)
{
int sum=0;
int res=0;
for(int j=i;j<vec.size();j++)
{
sum+=vec[j];
if(sum<=maxCost)
{
res++;
cnt=max(cnt,res);
}
else
{
cnt=max(cnt,res);
break;
}
}
}
return cnt;
}
};
边栏推荐
- Full analysis of embedded software testing tool tpt18 update
- How the innovative use of adobe international certification 𞓜 3D changes the entire industry
- JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
- Weekly Postgres world news 2022w02
- PTA:7-85 数据的间距问题(重载+函数模板)
- 什么是元数据
- Sequence table lookup
- Bug STM32 interrupt (everyone knows)
- 自媒体时代的贤内助——AI 视频云
- [advanced binary tree] AVLTree - balanced binary search tree
猜你喜欢

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

AI video cloud vs narrowband HD, who is the favorite in the video Era

深度学习 TensorFlow入门

Implementation of VGA protocol based on FPGA
![[advanced binary tree] AVLTree - balanced binary search tree](/img/a5/aef68dd489ef5545e5b11ee2d3facc.png)
[advanced binary tree] AVLTree - balanced binary search tree

How MySQL deletes a row of data in a table

Deploying Apache pulsar on kubesphere

支持在 Kubernetes 运行,添加多种连接器,SeaTunnel 2.1.2 版本正式发布!

Getting started with tensorflow

两招提升硬盘存储数据的写入效率
随机推荐
众昂矿业:新能源新材料产业链对萤石需求大增
How to solve the problem that the web page fails to log in after the easycvr service is started?
顺序表查找
Pyspark, paid for data cleaning and uploading to the database
Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!
Differences between MyISAM and InnoDB of MySQL storage engine
QMainWindow
Web page dynamic and static separation based on haproxy
How to make the page number start from the specified page in word
Svg+js smart home monitoring grid layout
电商如何借助小程序发力
PTA:7-86 集合的模拟实现(函数模板)
What is the open source database under Linux
How the innovative use of adobe international certification 𞓜 3D changes the entire industry
SVG+JS智能家居监控网格布局
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
Avltree - arbre de recherche binaire équilibré
The tax software exits when it detects that it is a virtual machine. How to solve this problem?
Full analysis of embedded software testing tool tpt18 update
Similar to RZ / SZ, trzsz supporting TMUX has released a new version