Skip to content

Instantly share code, notes, and snippets.

@tirjok
Last active July 3, 2016 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tirjok/012e56cd36e8e0284e2488b6fb5ac838 to your computer and use it in GitHub Desktop.
Save tirjok/012e56cd36e8e0284e2488b6fb5ac838 to your computer and use it in GitHub Desktop.
<?php
$_fp = fopen("php://stdin", "r");
$str = trim(fgets($_fp));
$str = str_split($str);
function removeAdjacent($s, $len)
{
$j = 0;
for ($i = 1; $i < $len; $i++) {
while (($j >= 0) && $i < $len && $s[$i] == $s[$j]) {
$i++;
$j--;
}
if ($i < $len) {
$s[++$j] = $s[$i];
}
}
$uniqChar = [];
for ($k = 0; $k <= $j; $k++) {
$uniqChar[$k] = $s[$k];
}
return $uniqChar;
}
$str = removeAdjacent($str, count($str));
if (count($str)) {
echo implode('', $str);
} else {
echo 'Empty String';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment