class
GFG {
static
int
startPos = -
1
, endPos = -
1
;
static
boolean
isValid(
int
len, String s, String t,
int
conversionCost,
int
maxCost)
{
int
start =
0
, end =
0
, n = s.length();
int
currCost =
0
;
while
(end < n) {
if
(s.charAt(end) != t.charAt(end)) {
currCost += conversionCost;
}
if
(end - start +
1
== len) {
if
(currCost <= maxCost) {
startPos = start;
endPos = end;
return
true
;
}
if
(s.charAt(start) != t.charAt(start)) {
currCost -= conversionCost;
}
start++;
end++;
}
else
{
end++;
}
}
return
false
;
}
static
void
validSubstring(String source, String target,
int
conversionCost,
int
maxCost)
{
int
low =
1
;
int
high = source.length();
while
(low <= high) {
int
mid = (low + high) /
2
;
if
(isValid(mid, source, target, conversionCost,
maxCost)) {
low = mid +
1
;
}
else
{
high = mid -
1
;
}
}
if
(startPos == -
1
)
System.out.println(
"Not possible\n"
);
else
System.out.println(startPos +
" "
+ endPos
+
"\n"
);
}
public
static
void
main(String[] args)
{
String source =
"adcf"
, target =
"cdef"
;
int
conversionCost =
3
, maxCost =
5
;
validSubstring(source, target, conversionCost,
maxCost);
}
}