URL
https://leetcode.com/problems/get-equal-substrings-within-budget/description/?envType=daily-question&envId=2024-05-28
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202405/get_equal_substrings_within_budget/main.fsx
let equalSubstring (s: string) (t: string) (maxCost: int) : int =
let rec adjustCost cost left maxCost (s: char[]) (t: char[]) =
if cost <= maxCost then
cost, left
else
let cost' = cost - ((int s.[left] - int t.[left]) |> abs)
adjustCost cost' (left + 1) maxCost s t
let rec equalSubstring' i (s: char[]) (t: char[]) left maxCost cost ret =
if i >= s.Length then
ret
else
let cost' = cost + ((int s.[i] - int t.[i]) |> abs)
let cost'', left' = adjustCost cost' left maxCost s t
equalSubstring' (i + 1) s t left' maxCost cost'' (max ret (i - left' + 1))
equalSubstring' 0 (s |> Seq.toArray) (t |> Seq.toArray) 0 maxCost 0 0