program AnagramDiff;
const
TotalABCLetters = 26;
type
TCountArray = array[0..TotalABCLetters-1] of Integer;
procedure PrintCharactersNeedToBeRemovedForAnagram(str1, str2: string);
var
count1, count2: TCountArray;
i, idx: Integer;
begin
{ initialize arrays }
for i := 0 to TotalABCLetters-1 do
begin
count1[i] := 0;
count2[i] := 0;
end;
{ count char frequency in str1 }
for i := 1 to Length(str1) do
begin
idx := Ord(str1[i]) - Ord('a');
if (idx >= 0) and (idx < TotalABCLetters) then
Inc(count1[idx]);
end;
{ count char frequency in str2 }
for i := 1 to Length(str2) do
begin
idx := Ord(str2[i]) - Ord('a');
if (idx >= 0) and (idx < TotalABCLetters) then
Inc(count2[idx]);
end;
{ print differing characters }
for i := 0 to TotalABCLetters-1 do
if Abs(count1[i] - count2[i]) <> 0 then
Write(Chr(i + Ord('a')), ' ');
end;
var
str1, str2: string;
begin
str1 := 'masterfx';
str2 := 'ksampret';
PrintCharactersNeedToBeRemovedForAnagram(str1, str2);
end.
(*
run:
f k p x
*)