当前位置:网站首页>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;
}
};
边栏推荐
- Introduction to deep learning
- Introduction and use of MySQL view
- 给你的AppImage创建桌面快捷方式
- Form development mode
- How does flutter achieve different zoom animation effects
- PTA:6-30 时间相加
- P1347 排序(topo)
- PTA: Simulation Implementation of 7-86 set (function template)
- The spring recruitment in 2022 begins, and a collection of interview questions will help you
- IDEA-导入模块
猜你喜欢

摆烂LuoGu刷题记

Xiaojinwei, chairman of Chenglian Technology: implement the national strategy of data economy and lead the development of new consumption in the digital era!

It supports running in kubernetes, adds multiple connectors, and seatunnel version 2.1.2 is officially released!

Implementation of VGA protocol based on FPGA

浅析2022年物联网现状

Svg+js smart home monitoring grid layout

LabVIEW在同一表中同时显示十六进制字符和普通字符

Software development in 2022: five realities CIOs should know

理想汽车×OceanBase:当造车新势力遇上数据库新势力

Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection
随机推荐
Static lookup tables and static lookup tables
Zhongang Mining: the demand for fluorite in the new energy and new material industry chain has increased greatly
自动化测试常见的面试题
mysql如何删除表的一行数据
Sequence table lookup
抖音x-bogus和_signature参数分析
给你的AppImage创建桌面快捷方式
IDEA-导入模块
photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
Mobile terminal city list sorting JS plug-in vertitylist js
② cocoapods原理及 PodSpec 文件上传操作
摆烂LuoGu刷题记
Introduction and use of MySQL view
Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors
[pycharm] ide Eval resetter
Latest programming language rankings
解决使用Exception抛出后,@Transactional不生效
QMainWindow
What is metadata
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险