Two Characters

In this challenge, you will be given a string. You must remove characters until the string is made up of any two alternating characters. When you choose a character to remove, all instances of that character must be removed. Your goal is to create the longest string possible that contains just two alternating letters.
As an example, consider the string abaacdabd. If you delete the character a, you will be left with the string bcdbd. Now, removing the character c leaves you with a valid string bdbd having a length of 4. Removing either b or d at any point would not result in a valid string.

Given a string , convert it to the longest possible string  made up only of alternating characters. Print the length of string  on a new line. If no string  can be formed, print  instead.
Function Description
Complete the alternate function in the editor below. It should return an integer that denotes the longest string that can be formed, or  if it cannot be done.
alternate has the following parameter(s):
  • s: a string
Input Format
The first line contains a single integer denoting the length of .
The second line contains string .
Constraints
Output Format
Print a single integer denoting the maximum length of  for the given ; if it is not possible to form string , print  instead.
Sample Input
10
beabeefeab
Sample Output
5
Explanation
The characters present in  are abe, and f. This means that  must consist of two of those characters and we must delete two others. Our choices for characters to leave are [a,b], [a,e], [a, f], [b, e], [b, f] and [e, f].
If we delete e and f, the resulting string is babab. This is a valid  as there are only two distinct characters (a and b), and they are alternating within the string.
If we delete a and f, the resulting string is bebeeeb. This is not a valid string  because there are consecutive e's present. Removing them would leave consecutive b's, so this fails to produce a valid string .
Other cases are solved similarly.
babab is the longest string we can create.
php
<?php

// Complete the alternate function below.
function alternate($s) {
$arr = str_split($s);
$uniquired = array_count_values($arr);
//$cnt = count($uniquired);
$mx = array();
foreach($uniquired as $k => $v) {
foreach($uniquired as $k1 => $v1) {
if($v - $v1 == 1) {
$key = array_keys($arr, $k);
$key1 = array_keys($arr, $k1);
$lck = false;
$cnt = count($key1);
$cnt1 = count($key);
for($i = 0; $i < $cnt; $i++) {
if($key[$i] > $key1[$i]) {
$lck = true;
break;
}
if($key1[$cnt-1] > $key[$cnt1-1]) {
$lck = true;
break;
}
if(!empty($key1[$i-1]) && $key[$i] < $key1[$i-1]) {
$lck = true;
break;
}
}
if(!$lck) {
$mx[] = count($key) + count($key1);
}
}
else if($v1 - $v == 1) {
$key = array_keys($arr, $k);
$key1 = array_keys($arr, $k1);
$lck = false;
$cnt = count($key);
$cnt1 = count($key1);
for($i = 0; $i < $cnt; $i++) {
if($key1[$i] > $key[$i]) {
$lck = true;
break;
}
if($key[$cnt-1] > $key1[$cnt1-1]) {
$lck = true;
break;
}
if(!empty($key[$i-1]) && $key1[$i] < $key[$i-1]) {
$lck = true;
break;
}
}
if(!$lck) {
$mx[] = count($key) + count($key1);
}
} else if ($v1 - $v == 0 && $k != $k1) {
$key = array_keys($arr, $k);
$key1 = array_keys($arr, $k1);
if($key[0] < $key1[0]) {
$lck = false;
$cnt = count($key1);
$cnt1 = count($key);
for($i = 0; $i < $cnt; $i++) {
if($key[$i] > $key1[$i]) {
$lck = true;
break;
}
if($key1[$cnt-1] < $key[$cnt1-1]) {
$lck = true;
break;
}
if(!empty($key1[$i-1]) && $key[$i] < $key1[$i-1]) {
$lck = true;
break;
}
}
if(!$lck) {
$mx[] = count($key) + count($key1);
}
} else {
$lck = false;
$cnt = count($key);
$cnt1 = count($key1);
for($i = 0; $i < $cnt; $i++) {
if($key1[$i] > $key[$i]) {
$lck = true;
break;
}
if($key[$cnt-1] < $key1[$cnt1-1]) {
$lck = true;
break;
}
if(!empty($key[$i-1]) && $key1[$i] < $key[$i-1]) {
$lck = true;
break;
}
}
if(!$lck) {
$mx[] = count($key) + count($key1);
}
}
}
}
}
if(count($mx) == 0)
return 0;
return max($mx);
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$l = intval(trim(fgets(STDIN)));

$s = rtrim(fgets(STDIN), "\r\n");

$result = alternate($s);

fwrite($fptr, $result . "\n");

fclose($fptr);

Comments

Popular posts from this blog

Compare the Triplets

Forming a Magic Square

Plus Minus