function IsAnagram_delphius(const S1, S2: string; IgnoreSpaces: Boolean = True; ExceptionOnError: Boolean = False): Boolean;
const
zeroFreq: array[32..128] of Byte = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var
s1c, s1e, s2c, s2e: PChar;
freq: array[32..128] of Byte;
ch: Byte;
begin
if not IgnoreSpaces and (Length(S1) <> Length(S2)) then
Exit(False);
s1c := PChar(S1);
s1e := s1c + Length(S1);
s2c := PChar(S2);
s2e := s2c + Length(S2);
FillQWord(freq, 12, 0);
while s1c < s1e do
begin
ch := Ord(s1c^);
case ch of
32: if not IgnoreSpaces then Inc(freq[ch]);
65..90: Inc(freq[ch or $20]);
33..64, 91..127: Inc(freq[ch]);
else
if ExceptionOnError then
raise ERangeError.CreateFmt('Illegal character in S1 at position %d', [s1c - PChar(S1) + 1])
else Exit(False);
end;
Inc(s1c);
end;
while s2c < s2e do
begin
ch := Ord(s2c^);
case ch of
32: if not IgnoreSpaces then Dec(freq[ch]);
65..90: Dec(freq[ch or $20]);
33..64, 91..127: Dec(freq[ch]);
else
if ExceptionOnError then
raise ERangeError.CreateFmt('Illegal character in S2 at position %d', [s2c - PChar(S2) + 1])
else Exit(False);
end;
Inc(s2c);
end;
Result := CompareDWord(freq[32], zeroFreq[32], 24) = 0;
end;