-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternating Characters.php
More file actions
46 lines (35 loc) · 883 Bytes
/
Alternating Characters.php
File metadata and controls
46 lines (35 loc) · 883 Bytes
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
<?php
/**
* @author: syed ashraf ullah
* date: 30/04/2020
* problem: https://www.hackerrank.com/challenges/alternating-characters/problem
*/
// Complete the alternatingCharacters function below.
function alternatingCharacters($s) {
$s = str_split($s);
$match = $s[0];
$count = 0;
for ($i=1; $i < sizeof($s); $i++) {
if ($s[$i] == $match) {
$count++;
continue;
}
$match = $s[$i];
}
return $count;
}
// $s = 'AAAA';
// $result = alternatingCharacters($s);
// echo $result;
// return;
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $q);
for ($q_itr = 0; $q_itr < $q; $q_itr++) {
$s = '';
fscanf($stdin, "%[^\n]", $s);
$result = alternatingCharacters($s);
fwrite($fptr, $result . "\n");
}
fclose($stdin);
fclose($fptr);