-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-characters.php
57 lines (44 loc) · 1.03 KB
/
two-characters.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
function is_valid($str)
{
$last = null;
for ($x = 0, $l = strlen($str); $x < $l; $x++) {
if ($last == $str[$x]) {
return false;
}
$last = $str[$x];
}
return true;
}
/*
* Complete the 'alternate' function below.
*
* The function is expected to return an INTEGER.
* The function accepts STRING s as parameter.
*/
function alternate($s)
{
$chars = array_values(array_unique(str_split($s, 1)));
$l = count($chars);
if ($l < 2) {
return 0;
}
$max = 0;
for ($x = 0; $x < $l - 1; $x++) {
for ($y = $x + 1; $y < $l; $y++) {
$tmp = $chars;
unset($tmp[$x], $tmp[$y]);
$str = str_replace($tmp, '', $s);
if (is_valid($str)) {
$max = max($max, strlen($str));
}
}
}
return $max;
}
$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);