当前位置:网站首页>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;
}
};
边栏推荐
- 2022年烷基化工艺考题及模拟考试
- 【二叉树进阶】AVLTree - 平衡二叉搜索树
- P1347 sorting (TOPO)
- 华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
- Twitter cooperates with Shopify to introduce merchant products into twitter shopping
- Code refactoring Guide
- Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
- LabVIEW在同一表中同时显示十六进制字符和普通字符
- svg d3.js生成tree树状图
- PTA:6-33 学生排名表(析构函数)
猜你喜欢

京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”

移动端城市列表排序js插件vercitylist.js

What is metadata

Latest programming language rankings

A summary of PostgreSQL data types. All the people are here

Compilation, installation and global configuration section description of haproxy

在word里,如何让页码从指定页开始编号

Imitation 360 desktop suspended ball plug-in

Inscription of lougu brush

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
随机推荐
Lighthouse locally deployed TCA code analysis tool
photoshop PS 查看像素坐标、像素颜色、像素HSB颜色
PTA: Simulation Implementation of 7-87 set (class template)
虫子 STM32 中断 (懂的都懂)
IDEA-导入模块
Leetcode 1208. 尽可能使字符串相等(终于解决,晚安)
PTA:7-69 数据的间距问题
Pytoch --- use pytoch's pre training model to realize four weather classification problems
京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”
背景彩带动画插件ribbon.js
x64dbg 基本使用技巧
Introduction and use of MySQL view
Mobile terminal city list sorting JS plug-in vertitylist js
PTA:7-85 数据的间距问题(重载+函数模板)
Introduction to deep learning
How to process large volume xlsx/csv/txt files?
linux下的开源数据库是什么
Photoshop PS viewing pixel coordinates, pixel colors, pixel HSB colors
Particle animation background login page particles js
Pytorch---使用Pytorch的预训练模型实现四种天气分类问题