DEV Community

Discussion on: Daily Challenge #259 - Duplicate Encoder

Collapse
 
peter279k profile image
peter279k

Here is the simple approach with nested for loops with PHP:

function duplicate_encode($word){
  $index = 0;
  $word = mb_strtolower($word);
  $result = $word;
  for ($index=0; $index<mb_strlen($word); $index++) {
    $isDuplicated = false;
    for ($secondIndex=$index+1; $secondIndex<mb_strlen($word); $secondIndex++) {
      if ($word[$index] === $word[$secondIndex]) {
        $result[$index] = ")";
        $result[$secondIndex] = ")";
        $isDuplicated = true;   
      }
    }

    if ($isDuplicated === false && $result[$index] === $word[$index]) {
      $result[$index] = "(";
    }
  }

  return $result;
}