Remove Adjacent Duplicates
PHP
Hard
4 views
Problem Description
Repeatedly remove adjacent equal characters. Print the final string.
Input Format
One line string s.
Output Format
One line final string.
Official Solution
<?php
$inputText=rtrim(stream_get_contents(STDIN));
if($inputText==='') exit;
$st=[];
for($i=0,$n=strlen($inputText);$i<$n;$i++){
$ch=$inputText[$i];
if($st && $st[count($st)-1]===$ch) array_pop($st);
else $st[]=$ch;
}
echo implode('',$st);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!