当前位置:网站首页>Leetcode 1208. Make strings as equal as possible
Leetcode 1208. Make strings as equal as possible
2022-06-23 04:36: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 .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/get-equal-substrings-within-budget
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
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;
}
};
边栏推荐
- What is the difference between redistemplate and CacheManager operation redis
- 浅析2022年物联网现状
- photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
- 仿360桌面悬浮球插件
- svg d3. JS generate tree tree view
- mysql如何删除表的一行数据
- 【一起上水硕系列】Day Three - preview4
- Dynamics 365 插件中权限操作
- QMainWindow
- The spring recruitment in 2022 begins, and a collection of interview questions will help you
猜你喜欢

photoshop PS 查看像素坐标、像素颜色、像素HSB颜色

Particle animation background login page particles js

Svg+js smart home monitoring grid layout

There is a problem with redis startup

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

Online text filter less than specified length tool

How to use MySQL index well

给你的AppImage创建桌面快捷方式

Black horse PostgreSQL, why is it black in the end

Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!
随机推荐
How does flutter achieve different zoom animation effects
Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
怎么使用Shell脚本实现监测文件变化
PTA:7-85 数据的间距问题(重载+函数模板)
顺序表查找
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
浅析2022年物联网现状
There is a problem with redis startup
Avltree - arbre de recherche binaire équilibré
无线网络安全的12个优秀实践
基于FPGA的VGA协议实现
给你的AppImage创建桌面快捷方式
虫子 STM32 高级定时器 (哈哈我说实话硬件定时器不能体现实力,实际上想把内核定时器发上来的,一想算了,慢慢来吧)
Introduction and use of MySQL view
Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!
Section 2: spingboot unit test
会话和守护进程
3D数学基础[十六] 匀加速直线运动的公式
Redis启动有问题
What is the difference between redistemplate and CacheManager operation redis