题目如下:

You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.

You are also given an integer maxCost.

Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.

If there is no substring from s that can be changed to its corresponding substring from t, return 0.

Example 1:

Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.

Example 2:

Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.

Example 3:

Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1. 

Constraints:

  • 1 <= s.length, t.length <= 10^5
  • 0 <= maxCost <= 10^6
  • s and t only contain lower case English letters.

解题思路:本题包了一层壳,去掉外表后题目是给定一个正整数组成的数组,求出最长的一段子数组的长度,要求子数组的和不大于cost。解题方法也不难,记per_cost[i]为abs(s[i] - t[i])的值,cost[i]为sum(per_cost[0:i])的值。对于任意一个下标i,很容易通过二分查找的方法找出cost中另外一个下标j,使得cost[i:j] <= cost。

代码如下:

class Solution(object):
def equalSubstring(self, s, t, maxCost):
"""
:type s: str
:type t: str
:type maxCost: int
:rtype: int
"""
cost = []
amount = 0
per_cost = []
for cs,ct in zip(s,t):
amount += abs(ord(cs) - ord(ct))
cost.append(amount)
per_cost.append(abs(ord(cs) - ord(ct)))
#cost.sort()
#print cost
#print per_cost
import bisect
res = -float('inf')
for i in range(len(cost)):
inx = bisect.bisect_right(cost,cost[i] + maxCost - per_cost[i])
res = max(res,inx - i)
return res

【leetcode】1208. Get Equal Substrings Within Budget的更多相关文章

  1. 【LeetCode】1208. 尽可能使字符串相等 Get Equal Substrings Within Budget (Python)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 示例 解题思路 滑动窗口 代码 刷题心 ...

  2. 【LeetCode】696. Count Binary Substrings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...

  3. 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...

  4. 【leetcode】927. Three Equal Parts

    题目如下: Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these ...

  5. 【leetcode】1224. Maximum Equal Frequency

    题目如下: Given an array nums of positive integers, return the longest possible length of an array prefi ...

  6. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  7. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  8. 【leetcode】698. Partition to K Equal Sum Subsets

    题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...

  9. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

随机推荐

  1. 【Ruby on Rails 学习三】Ruby 基本数据类型(类、类的实例、对象)

    数字.文本.范围.符合.True.False.Nil 1为什么是一个类的对象,使用methods方法可以查看一个对象的所有函数(方法) $ irb irb(main)::> => irb( ...

  2. 接口自动化--数据加密之AES

    在接口测试中,会遇到加密的请求数据,例如:常用的base64加密,AES加密,在这里,简述用Python转化AES的加密方法 原理 官网链接:https://pycryptodome.readthed ...

  3. poj1220(短除法实现任意进制转换)

    题目链接:https://vjudge.net/problem/POJ-1220 题意:给定a进制的大数s,将其转换为b进制.其中2<=a,b<=62. 题意:一般进制转换是以10进制为中 ...

  4. springboot - 应用实践(3)springboot的核心

    1.springboot的启动类与核心注解@SpringBootApplication 2.springboot基本配置 3.springboot自动配置原理

  5. 小记----采集之Xpath

    一.XPATH简介   Xpath是XML路径语言,它是一种确定XML文档中某部分位置的语言 二.XPATH语法  XPATH使用路径表达式在XML文档中选取节点.节点是通过沿着路径或者step来选取 ...

  6. iOS模拟器发生了崩溃,去哪找Crash Log

    iOS模拟器发生了崩溃,可以在如下地方找到崩溃日志: ~/Library/Logs/DiagnosticReports/

  7. SpringBoot_01

    一.初识springboot 个人总结:springboot是一个开发更加便捷的spring的技术框架,通过引入启动器便可以快捷的让spring框架和其他框架进行整合, springboot很容易上手 ...

  8. P1880石子合并

    1995年的noi区间dp题,这道题AC耗时达到了数月. 有一道题叫做果子合并,也是求合并的最小花费,但是那个题是可以随便合并两堆,但是这个题只能合并相邻的两堆,并且是一个环.对于环的问题,我们一般可 ...

  9. 闭包、装饰器decorator、迭代器与生成器、面向过程编程、三元表达式、列表解析与生成器表达式

    一.装饰器 一.装饰器的知识储备 不想修改函数的调用方式,但是还想在原来的函数前后添加功能 1.可变长参数  :*args和**kwargs def index(name,age): print(na ...

  10. const关键字 C与C++分析

    1 C与C++的区别 1.1.C允许定义两个变量名相同的变量,而C++不允许. 在C语言中是允许定义两个名字相同的全局变量.  在C++中是不允许定义两个名字相同的全局变量. 测试代码: /* 编译环 ...