Anagram Substring Search problem in F#

One inter­est­ing prob­lem I read about as I pre­pare for var­i­ous tech­ni­cal inter­views is the so called Ana­gram Sub­string Search prob­lem where it can be boiled down to the ques­tion “find all per­mu­ta­tions of s with­in b”.

Nat­u­ral­ly, after all my endeav­ours with Project Euler and Advent of Code, I cast my mind to how I’d solve this in F#, and it turned out eas­i­er than I imag­ined 

The trick is that if you sort the char­ac­ters in both strings alpha­bet­i­cal­ly then you can lever­age F#‘s struc­tur­al com­par­i­son for arrays to check if the two strings are ana­grams. Essen­tial­ly, you only need to answer the ques­tion “do the same char­ac­ters exist in both strings?”, and sort­ing both strings the same way lets you achieve that real­ly eas­i­ly.


Try it Yourself



Liked this post? Why not support me on Patreon and help me get rid of the ads!