Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 1544. Make The String Great in F#

Shohei Yoshida's photo
Shohei Yoshida
·Nov 8, 2022·

1 min read

URL

leetcode.com/problems/make-the-string-great

Code

github.com/syohex/dotnet-study/blob/master/..

let makeGood (s: string) : string =
    let rec makeGood' cs (acc: char list) =
        match cs with
        | [] -> acc |> List.rev |> System.String.Concat
        | h :: t ->
            match acc with
            | [] -> makeGood' t (h :: acc)
            | prev :: rest ->
                if (System.Char.IsLower(h)
                    && prev = System.Char.ToUpper(h))
                   || (System.Char.IsUpper(h)
                       && prev = System.Char.ToLower(h)) then
                    makeGood' t rest
                else
                    makeGood' t (h :: acc)

    makeGood' (s |> Seq.toList) []
 
Share this