![]() |
VOOZH | about |
Given two strings source and target of equal length, and two positive integers, maxCost, and conversionCost, the task is to find the indices of the longest substring in source that can be converted into the same substring in target with a cost less than or equal to maxCost, where each character conversion having a cost of conversionCost units.
Note: If multiple valid substrings exist, return any one of them.
Examples:
Input: source = "abcd", target = "bcdf", conversionCost = 1, maxCost = 3
Output: 0 2
Explanation: The substring of source from index 0 to 2 is "abc" which can change to "bcd" in target. The cost of changing would be 3 <= maxCost. And the maximum length possible is 3.Input: source = "adcf", target = "cdef", conversionCost = 3, maxCost = 5
Output: 1 3
The idea is to use binary search on answer to find the maximum possible length, and for each length, check its possible to change within maxCost and based on that update the search space of answer and keep track of indices of start and end position of valid substring.
Step-by-step approach:
Below is the implementation of the above approach.
1 3
Time Complexity: O(N* log(N))
Auxiliary Space: O(1)