cft
Become a CreatorSign inGet Started

LeetCode 389. Find the Difference in F#

Solve daily LeetCode problem in F#


user

Shohei Yoshida

4 months ago | 1 min read
Follow

leetcode-389-find-difference-f-ttzr7

URL

https://leetcode.com/problems/find-the-difference/

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/problems/0389/main.fsx

let strToMap (s: string) : Map<char, int> =

s

|> Seq.toList

|> List.fold

(fun m c ->

match Map.tryFind c m with

| Some count -> Map.add c (count + 1) m

| None -> Map.add c 1 m)

Map.empty

let findTheDifference (s: string) (t: string) : char =

let rec findTheDifference' cs sm tm =

match cs with

| [] -> failwith "never reach here"

| head :: tail ->

match (Map.tryFind head sm), (Map.tryFind head tm) with

| Some a, Some b when a <> b -> head

| Some _, Some _ -> findTheDifference' tail sm tm

| None, Some _ -> head

| _, _ -> failwith "never reach here"

let sm = strToMap s

let tm = strToMap t

findTheDifference' (tm |> Map.keys |> Seq.toList) sm tm

Upvote


user
Created by

Shohei Yoshida

Follow

A programmer of DeNA

A Programmer


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles