Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 242. Valid Anagram in F#

Shohei Yoshida's photo
Shohei Yoshida
·Jul 28, 2022·

1 min read

URL

leetcode.com/problems/valid-anagram

Code

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

let isAnagram (s: string) (t: string) : bool =
    let rec isAnagram' ss tt =
        match ss, tt with
        | [], [] -> true
        | [], _
        | _, [] -> false
        | h1 :: t1, h2 :: t2 ->
            if h1 = h2 then
                isAnagram' t1 t2
            else
                false

    isAnagram' (s |> Seq.sort |> Seq.toList) (t |> Seq.sort |> Seq.toList)
 
Share this