Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 1047. Remove All Adjacent Duplicates In String in F#

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

1 min read

URL

leetcode.com/problems/remove-all-adjacent-d..

Code

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

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

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