-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1208. Get Equal Substrings Within Budget #256
Comments
The problem asks us to find the maximum length of a substring in string Key Points:
Approach:
Plan:
Let's implement this solution in PHP: 1208. Get Equal Substrings Within Budget <?php
/**
* @param String $s
* @param String $t
* @param Integer $maxCost
* @return Integer
*/
function equalSubstring($s, $t, $maxCost) {
$j = 0;
for ($i = 0; $i < strlen($s); ++$i) {
$maxCost -= abs(ord($s[$i]) - ord($t[$i]));
if ($maxCost < 0)
$maxCost += abs(ord($s[$j]) - ord($t[$j++]));
}
return strlen($s) - $j;
}
// Example usage:
$s = "abcd";
$t = "bcdf";
$maxCost = 3;
echo equalSubstring($s, $t, $maxCost) . "\n"; // Output: 3
$s = "abcd";
$t = "cdef";
$maxCost = 3;
echo equalSubstring($s, $t, $maxCost) . "\n"; // Output: 1
$s = "abcd";
$t = "acde";
$maxCost = 0;
echo equalSubstring($s, $t, $maxCost) . "\n"; // Output: 1
?> Explanation:
Example Walkthrough:Example 1: $s = "abcd";
$t = "bcdf";
$maxCost = 3;
Example 2: $s = "abcd";
$t = "cdef";
$maxCost = 3;
Time Complexity:
Output for Example:Example 1: $s = "abcd";
$t = "bcdf";
$maxCost = 3;
Example 2: $s = "abcd";
$t = "cdef";
$maxCost = 3;
The sliding window approach efficiently solves the problem by maintaining a valid substring whose transformation cost does not exceed |
…t submissions 1270537145 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
…t submissions 1270537145 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
…t submissions 1270537145 Co-authored-by: kovatz <[email protected]> Co-authored-by: topugit <[email protected]> Co-authored-by: basharul-siddike <[email protected]> Co-authored-by: hafijul233 <[email protected]>
Discussed in #255
Originally posted by mah-shamim August 9, 2024
Topics:
String
,Binary Search
,Sliding Window
,Prefix Sum
You are given two strings
s
andt
of the same length and an integermaxCost
.You want to change
s
tot
. Changing theith
character of s to ith character oft
costs|s[i] - t[i]|
(i.e., the absolute difference between the ASCII values of the characters).Return the maximum length of a substring of
s
that can be changed to be the same as the corresponding substring oft
with a cost less than or equal tomaxCost
. If there is no substring froms
that can be changed to its corresponding substring fromt
, return0
.Example 1:
That costs 3, so the maximum length is 3.
Example 2:
Example 3:
Constraints:
1 <= s.length <= 105
t.length == s.length
0 <= maxCost <= 106
s
andt
consist of only lowercase English letters.Hint:
The text was updated successfully, but these errors were encountered: