LeetCode 438. Find All Anagrams in a String in F#
URL
https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
Code
let toFreq (cs: char list) : int array =
cs
|> List.fold
(fun acc c ->
let index = int c - int 'a'
acc.[index] <- acc.[index] + 1
acc)
(Array.zeroCreate 26)
let findAnagrams (s: string) (p: string) : int list =
if p.Length > s.Length then
[]
else
let pFreq = toFreq (p |> List.ofSeq)
s
|> List.ofSeq
|> List.windowed p.Length
|> List.map toFreq
|> List.indexed
|> List.filter (fun (i, window) -> pFreq = window)
|> List.map fst