DEV Community

Discussion on: CodeToday: "Word Split" Algorithm, Coderbyte

Collapse
 
irsh5 profile image
irsh5

If you want the solution for PHP language, then you can use below code:

function ArrayChallenge($strArr) {

  $dictWords = explode( ',', $strArr[1] );
  $strLength = strlen($strArr[0]);

  $output = 'not possible';

  for( $i = 1; $i < $strLength; $i++ ){

    $firstStr = substr($strArr[0], 0, $i);
    $lastStr = substr($strArr[0], $i, $strLength);

    if ( in_array( $firstStr, $dictWords ) && in_array( $lastStr, $dictWords ) ) {
      $output = $firstStr . ',' . $lastStr;
      break;
    }

  }

  return $output;

}
Enter fullscreen mode Exit fullscreen mode