Same Substring
Learn this problemProblem statement
Two strings, s, and t, each of length n, that contain lowercase English characters are given as well as an integer K.
The cost to change the ith character in s from s[i] to t[i] is the absolute difference of the ASCII value of characters, i.e., abs(s[i] - t[i]).
Find the maximum length of a substring of s that can be changed to the corresponding substring of t with a total cost less than or equal to K. If there is no such substring, return 0.
Function
sameSubstring(s: String, t: String, K: int) → int
Complete the function sameSubstring in the editor.
sameSubstring has the following parameters:
String s: the string to alterString t: the string to matchint K: the maximum sum of costs
Returns
int: the maximum length of a substring that can be changed
🌷༊·˚, Credit to ʚrobotɞ ᡣ𐭩
Examples
Example 1
s = "adpgki"t = "cdmxki"K = 6return = 3
- Change s0 from 'a' to 'c' with cost = abs('a' - 'c') = 2.
String s is now "cdpgki" and K = 6 - 2 = 4.
- Change s2 from 'p' to 'm' with cost = abs('p' - 'm') = 3.
String s is "cdmgkji" and K = 4 - 3 = 1.
- The only character left to change is 'g' to 'x', which costs more than K.
The answer is 3.
More Salesforce problems
- Minimize Total Input Cost (for LTMS)Seen Jun 2026
- Count Prime StringsONSITE INTERVIEW · Seen Jun 2026
- Final Pod Counts After LogsOA · Seen May 2026
- ATM Queue Exit OrderPHONE SCREEN · Seen May 2026
- Good Ways to Split an ArrayPHONE SCREEN · Seen May 2026
- Generate Seen Binary StringsOA · Seen May 2026
- Update Pod Counts From LogsOA · Seen May 2026
- Minimum Removals to Balance ArrayOA · Seen May 2026