-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGemstones.php
More file actions
65 lines (50 loc) · 1.43 KB
/
Gemstones.php
File metadata and controls
65 lines (50 loc) · 1.43 KB
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
58
59
60
61
62
63
64
65
<?php
/**
* @author: syed ashraf ullah
* date: 30/04/2020
* problem: https://www.hackerrank.com/challenges/gem-stones/problem
*/
// Complete the gemstones function below.
function gemstones($arr) {
// Define a array of size a-z => 97-123 which default value is zero
$gem = array_fill(97,123,0);
$size = sizeof($arr);
for ($i=0; $i < $size; $i++) {
$stone = str_split($arr[$i]);
$s = sizeof($stone);
foreach ($stone as $key => $value) {
// A character will only be gem when it present on all previous stone.
// Increase the index value one when it also present on all previous stone.
if ($gem[ord($value)] == $i) {
$gem[ord($value)] = $i+1;
}
}
}
$count = 0;
foreach ($gem as $value) {
// Index value == total stone => gem
if($value == $size) $count++;
}
return $count;
}
/**
* Sample #1
*/
// $arr = array('abcdde', 'baccd', 'eeabg');
// $arr = array('basdfj', 'asdlkjfdjsa', 'bnafvfnsd', 'oafhdlasd');
// $result = gemstones($arr);
// echo $result;
// return;
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
$arr = array();
for ($i = 0; $i < $n; $i++) {
$arr_item = '';
fscanf($stdin, "%[^\n]", $arr_item);
$arr[] = $arr_item;
}
$result = gemstones($arr);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);