Yep, stack-based trick worked just fine:
let reactQuickly input = Seq.fold (fun s c -> let last = if Array.length s > 0 then Some (Array.last s) else None match last with | Some x -> if c <> x && (x = System.Char.ToUpper c || x = System.Char.ToLower c) then Array.sub s 0 (Array.length s - 1) else Array.append s [| c |] | None -> Array.append s [| c |]) [| |] input |> Array.length
Replacing reactString with reactQuickly was enough to bring the total runtime from several (many) minutes to around 3 seconds.
reactString
reactQuickly
Using the tip from @choroba and reducing everything first would probably speed it up even more.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yep, stack-based trick worked just fine:
Replacing
reactString
withreactQuickly
was enough to bring the total runtime from several (many) minutes to around 3 seconds.Using the tip from @choroba and reducing everything first would probably speed it up even more.