How to remove all adjacent duplicate characters from a string until no more can be removed in Pascal

1 Answer

0 votes
program RemoveAdjDuplicatesProgram;

function RemoveAdjacentDuplicates(s: string): string;
var
  stack: string;
  i: integer;
begin
  stack := '';

  for i := 1 to Length(s) do
  begin
    if (Length(stack) > 0) and (stack[Length(stack)] = s[i]) then
      Delete(stack, Length(stack), 1)   { pop }
    else
      stack := stack + s[i];            { push }
  end;

  RemoveAdjacentDuplicates := stack;
end;

var
  s: string;
begin
  s := 'abbacccada';
  
  WriteLn(RemoveAdjacentDuplicates(s));   { prints: cada }
end.




(*
run:

cada

*)

 



answered Mar 7 by avibootz

Related questions

...