1 Star 0 Fork 1

hesuyang/php-leetcode

forked from shyiran/php-leetcode 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
leetcode-1208-get-equal-substrings-within-budget.php 1.92 KB
一键复制 编辑 原始数据 按行查看 历史
shyiran 提交于 2023-01-10 10:04 +08:00 . 补充
<?php
/**
* @Time: 2021/2/5
* @DESC: 1208. 尽可能使字符串相等
* 给你两个长度相同的字符串,s 和 t。
* 将 s 中的第 i 个字符变到 t 中的第 i 个字符需要 |s[i] - t[i]| 的开销(开销可能为 0),也就是两个字符的 ASCII 码值的差的绝对值。
* 用于变更字符串的最大预算是 maxCost。在转化字符串时,总开销应当小于等于该预算,这也意味着字符串的转化可能是不完全的。
* 如果你可以将 s 的子字符串转化为它在 t 中对应的子字符串,则返回可以转化的最大长度。
* 如果 s 中没有子字符串可以转化成 t 中对应的子字符串,则返回 0。
* 示例 1:
输入:s = "abcd", t = "bcdf", cost = 3
输出:3
解释:s 中的 "abc" 可以变为 "bcd"。开销为 3,所以最大长度为 3。
* @param $s
* @param $t
* @param $maxCost
* @return int|mixed
* @link: https://leetcode-cn.com/problems/get-equal-substrings-within-budget/
*/
function equalSubstring($s, $t, $maxCost) {
$len = strlen($s);
if ($s == $t) return $len;
$maxLen = 0;
$s = stringToAsciiArr($s);
$t = stringToAsciiArr($t);
$current_sum = 0;
$left = $right = 0;
while ($right < $len) {
if ($right < $len) {
$current_sum += abs($s[$right] - $t[$right]);
$right++;
}
while ($current_sum > $maxCost && $left < $len) {
$current_sum -= abs($s[$left] - $t[$left]);
$left++;
}
if ($current_sum <= $maxCost) {
$maxLen = max($maxLen, $right - $left);
}
if ($right == $len) $left++;
}
return $maxLen;
}
function stringToAsciiArr($str) {
$arr = str_split($str);
foreach ($arr as $k => $item) {
$arr[$k] = ord($item);
}
return $arr;
}
$s = 'abcd';
$t = 'abcd';
$maxCost = 1;
$t = "krrgw";
$s = "zjxss";
$maxCost = 19;
var_dump(equalSubstring($s,$t,$maxCost));
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/hesuyang/php-leetcode.git
git@gitee.com:hesuyang/php-leetcode.git
hesuyang
php-leetcode
php-leetcode
master

搜索帮助