Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Created July 31, 2023 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keturiosakys/fd8f94eb3cca901a1c7ff302fd65bbcd to your computer and use it in GitHub Desktop.
Save keturiosakys/fd8f94eb3cca901a1c7ff302fd65bbcd to your computer and use it in GitHub Desktop.
Given two strings s and t, return true if t is an anagram of s, and false otherwise. Try this in a language you're not comfortable with!
open Base
open Stdio
(*Given two strings s and t, return true if t is an anagram of s, and false otherwise. Try
this in a language you're not comfortable with! *)
let is_anagram t s =
let sorter a b = Char.compare a b in
let t_sorted = String.to_list t |> List.sort ~compare:sorter in
let s_sorted = String.to_list s |> List.sort ~compare:sorter in
match List.compare Stdlib.compare t_sorted s_sorted with
| 0 -> true
| _ -> false
;;
let%expect_test "test is_anagram" =
is_anagram "barbie" "oppenheimer" |> Bool.to_string |> print_endline;
[%expect {| false |}]
;;
let%expect_test "test is_anagram" =
is_anagram "race" "care" |> Bool.to_string |> print_endline;
[%expect {| true |}]
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment