1208. Get Equal Substrings Within Budget Medium

@problem@discussion
#String#Binary Search#Sliding Window#Prefix Sum



1/**
2 * [1208] Get Equal Substrings Within Budget
3 *
4 * You are given two strings s and t of the same length and an integer maxCost.
5 * 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]| (i.e., the absolute difference between the ASCII values of the characters).
6 * Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of t with 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.
7 *  
8 * Example 1:
9 * 
10 * Input: s = "abcd", t = "bcdf", maxCost = 3
11 * Output: 3
12 * Explanation: "abc" of s can change to "bcd".
13 * That costs 3, so the maximum length is 3.
14 * 
15 * Example 2:
16 * 
17 * Input: s = "abcd", t = "cdef", maxCost = 3
18 * Output: 1
19 * Explanation: Each character in s costs 2 to change to character in t,  so the maximum length is 1.
20 * 
21 * Example 3:
22 * 
23 * Input: s = "abcd", t = "acde", maxCost = 0
24 * Output: 1
25 * Explanation: You cannot make any change, so the maximum length is 1.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	1 <= s.length <= 10^5
31 * 	t.length == s.length
32 * 	0 <= maxCost <= 10^6
33 * 	s and t consist of only lowercase English letters.
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/get-equal-substrings-within-budget/
39// discuss: https://leetcode.com/problems/get-equal-substrings-within-budget/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn equal_substring(s: String, t: String, max_cost: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_1208() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.