-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
CheckAnagrams.fs
40 lines (37 loc) · 1.17 KB
/
CheckAnagrams.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace Algorithms.Strings
/// wiki: https://en.wikipedia.org/wiki/Anagram
module CheckAnagrams =
/// <summary>
/// Two strings are anagrams if they are made of the same letters
/// arranged differently (ignoring the case).
/// </summary>
/// <example>
/// <code>
/// check_anagrams('Silent', 'Listen')
/// True
///
/// check_anagrams('This is a string', 'Is this a string')
/// True
///
/// check_anagrams('This is a string', 'Is this a string')
/// True
///
/// check_anagrams('There', 'Their')
/// False
/// </code>
/// </example>
/// <param name="string1">First string</param>
/// <param name="string2">Second string</param>
/// <returns>Boolean</returns>
let isAnagram (string1: string, string2: string): bool =
let a =
string1.ToLower().ToCharArray()
|> Array.filter (fun chars -> chars <> ' ')
|> Array.sort
|> System.String.Concat
let b =
string2.ToLower().ToCharArray()
|> Array.filter (fun chars -> chars <> ' ')
|> Array.sort
|> System.String.Concat
a = b