-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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