Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,708 questions

55,472 answers

573 users

How to print the characters need to be removed so that two strings become anagram in Pascal

1 Answer

0 votes
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

*)

 



answered Mar 2 by avibootz
...