Given two strings S1 and S2 of length n. Additionally, two positive integers, target and C. The task is to determine the maximum length of a contiguous substring in S1 such that, by changing any character in the substring of S1 to another character, the resulting substring matches the corresponding segment in S2, and the total cost of these changes is at most target, where the cost of transformation for each character is C units.
Note: If there are multiple valid substrings with the same maximum length, provide any one of them as the answer.
Examples:
Input: S1 = “abcd”, S2 = “bcdf”, C = 1, target = 3 Output: 1 3 Explanation: Substring of S1 from index 1 to 3 is “bcd” that can change to “cdf”. The cost of changing would be 3. So the maximum length is 3.
Input: S1 = “adcd”, S2 = “cdef”, C = 2, target = 5 Output: 1 3
Approach:
The idea is to use Binary Search on Answer method to find what can be the maximum possible length. For each potential length (from the midpoint of the current search range [start, end]), use a sliding window approach to check all substrings of that length (i.e, mid) in S1. Calculates the transformation cost for each substring and checks if it’s less than or equal to the target. If a valid substring is found, the starting and ending positions are updated, and the search continues with larger lengths. If no valid substring is found for a certain length, the search continues with smaller lengths.
Steps-by-step approach:
- Initialize a variable start with the minimum valid length substring possible i.e., 0.
- Initialize a variable end with the maximum valid length substring possible i.e., the size of the string itself.
- Initialize a variable result for storing the maximum length of valid substring
- While the start is less than the end, do the following:
- Calculate the mid = (start + end) /2
- Check if mid is the possible length for a valid substring
- Assign the result to mid
- Shift start to mid + 1.
- Otherwise, shift end to mid.
- If any valid substring is possible then print the starting index and the ending index of the valid substring.
Below is the impelementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int startPos = -1, endPos = -1;
bool isValid( int len, string& S1, string& S2, int C, int target)
{
int i = 0, j = 0, n = S1.size(), cost = 0;
bool flag = false ;
while (j < n) {
cost += ((S1[j] != S2[j]) ? C : 0);
if (j - i + 1 == len) {
if (cost <= target) {
flag = true ;
startPos = i;
endPos = j;
}
cost -= ((S1[j] != S2[j]) ? C : 0);
i++;
}
j++;
}
return flag;
}
void validSubstring(string S1, string S2, int C, int target)
{
startPos = -1, endPos = -1;
int start = 0;
int end = S1.size();
int n = S1.size();
int result = 0;
while (start < end) {
int mid = (start + end) / 2;
if (isValid(mid, S1, S2, C, target)) {
result = mid;
start = mid + 1;
}
else {
end = mid;
}
}
if (startPos == -1)
cout << "Not possible\n" ;
else
cout << startPos << " " << endPos << "\n" ;
}
int main()
{
string S1 = "abcd" , S2 = "bcdf" ;
int C = 1, target = 5;
validSubstring(S1, S2, C, target);
S1 = "abc" , S2 = "cdf" ;
C = 10, target = 3;
validSubstring(S1, S2, C, target);
S1 = "abcb" , S2 = "cdfb" ;
C = 2, target = 2;
validSubstring(S1, S2, C, target);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int startPos = - 1 , endPos = - 1 ;
static boolean isValid( int len, String S1, String S2, int C, int target) {
int i = 0 , j = 0 , n = S1.length(), cost = 0 ;
boolean flag = false ;
while (j < n) {
cost += (S1.charAt(j) != S2.charAt(j)) ? C : 0 ;
if (j - i + 1 == len) {
if (cost <= target) {
flag = true ;
startPos = i;
endPos = j;
}
cost -= (S1.charAt(i) != S2.charAt(i)) ? C : 0 ;
i++;
}
j++;
}
return flag;
}
static void validSubstring(String S1, String S2, int C, int target) {
startPos = - 1 ;
endPos = - 1 ;
int start = 0 ;
int end = S1.length();
int n = S1.length();
int result = 0 ;
while (start < end) {
int mid = (start + end) / 2 ;
if (isValid(mid, S1, S2, C, target)) {
result = mid;
start = mid + 1 ;
} else {
end = mid;
}
}
if (startPos == - 1 )
System.out.println( "Not possible" );
else
System.out.println(startPos + " " + endPos);
}
public static void main(String[] args) {
String S1 = "abcd" , S2 = "bcdf" ;
int C = 1 , target = 5 ;
validSubstring(S1, S2, C, target);
S1 = "abc" ; S2 = "cdf" ;
C = 10 ; target = 3 ;
validSubstring(S1, S2, C, target);
S1 = "abcb" ; S2 = "cdfb" ;
C = 2 ; target = 2 ;
validSubstring(S1, S2, C, target);
}
}
|
C#
using System;
class Program {
static int startPos = -1, endPos = -1;
static bool IsValid( int len, string S1, string S2,
int C, int target)
{
int i = 0, j = 0, n = S1.Length, cost = 0;
bool flag = false ;
while (j < n) {
cost += (S1[j] != S2[j]) ? C : 0;
if (j - i + 1 == len) {
if (cost <= target) {
flag = true ;
startPos = i;
endPos = j;
}
cost -= (S1[i] != S2[i]) ? C : 0;
i++;
}
j++;
}
return flag;
}
static void ValidSubstring( string S1, string S2, int C,
int target)
{
startPos = -1;
endPos = -1;
int start = 0;
int end = S1.Length;
while (start < end) {
int mid = (start + end) / 2;
if (IsValid(mid, S1, S2, C, target)) {
start = mid + 1;
}
else {
end = mid;
}
}
if (startPos == -1)
Console.WriteLine( "Not possible" );
else
Console.WriteLine(startPos + " " + endPos);
}
static void Main( string [] args)
{
string S1 = "abcd" , S2 = "bcdf" ;
int C = 1, target = 5;
ValidSubstring(S1, S2, C, target);
S1 = "abc" ;
S2 = "cdf" ;
C = 10;
target = 3;
ValidSubstring(S1, S2, C, target);
S1 = "abcb" ;
S2 = "cdfb" ;
C = 2;
target = 2;
ValidSubstring(S1, S2, C, target);
}
}
|
Javascript
let startPos = -1, endPos = -1;
function isValid(len, S1, S2, C, target) {
let i = 0, j = 0, n = S1.length, cost = 0;
let flag = false ;
while (j < n) {
cost += (S1[j] !== S2[j] ? C : 0);
if (j - i + 1 === len) {
if (cost <= target) {
flag = true ;
startPos = i;
endPos = j;
}
cost -= (S1[j] !== S2[j] ? C : 0);
i++;
}
j++;
}
return flag;
}
function validSubstring(S1, S2, C, target) {
startPos = -1;
endPos = -1;
let start = 0;
let end = S1.length;
let result = 0;
while (start < end) {
let mid = Math.floor((start + end) / 2);
if (isValid(mid, S1, S2, C, target)) {
result = mid;
start = mid + 1;
} else {
end = mid;
}
}
if (startPos === -1)
console.log( "Not possible" );
else
console.log(startPos + " " + endPos);
}
let S1 = "abcd" , S2 = "bcdf" ;
let C = 1, target = 5;
validSubstring(S1, S2, C, target);
S1 = "abc" , S2 = "cdf" ;
C = 10, target = 3;
validSubstring(S1, S2, C, target);
S1 = "abcb" , S2 = "cdfb" ;
C = 2, target = 2;
validSubstring(S1, S2, C, target);
|
Python3
def is_valid(length, S1, S2, C, target):
i, j, n, cost = 0 , 0 , len (S1), 0
flag = False
start_pos, end_pos = - 1 , - 1
while j < n:
cost + = (C if S1[j] ! = S2[j] else 0 )
if j - i + 1 = = length:
if cost < = target:
flag = True
start_pos, end_pos = i, j
cost - = (C if S1[j] ! = S2[j] else 0 )
i + = 1
j + = 1
return flag, start_pos, end_pos
def valid_substring(S1, S2, C, target):
start_pos, end_pos = - 1 , - 1
start, end, n = 0 , len (S1), len (S1)
result = 0
while start < end:
mid = (start + end) / / 2
is_valid_flag, i, j = is_valid(mid, S1, S2, C, target)
if is_valid_flag:
result = mid
start = mid + 1
start_pos, end_pos = i, j
else :
end = mid
if start_pos = = - 1 :
print ( "Not possible" )
else :
print (start_pos, end_pos)
S1, S2 = "abcd" , "bcdf"
C, target = 1 , 5
valid_substring(S1, S2, C, target)
S1, S2 = "abc" , "cdf"
C, target = 10 , 3
valid_substring(S1, S2, C, target)
S1, S2 = "abcb" , "cdfb"
C, target = 2 , 2
valid_substring(S1, S2, C, target)
|
Output
1 3
Not possible
2 3
Time Complexity: O(N* log(N)) Auxiliary Space: O(1)
|