-
Notifications
You must be signed in to change notification settings - Fork 0
/
HackerRank in a String.php
58 lines (45 loc) · 1.1 KB
/
HackerRank in a String.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
58
<?php
/**
* @author: syed ashraf ullah
* date: 29/04/2020
* problem: https://www.hackerrank.com/challenges/hackerrank-in-a-string/problem
*/
// Complete the hackerrankInString function below.
function hackerrankInString($s) {
// string to char array
$s = str_split($s);
$dictionary = array('h','a','c','k','e','r','r','a','n','k');
$size = sizeof($dictionary);
$index = array_fill (0,$size,0);
$i = 0;
foreach($s as $value){
if($value == $dictionary[$i]){
$index[$i] = 1;
$i++;
}
}
foreach($index as $index => $value ){
if(!$value) {
return 'NO';
}
}
return 'YES';
}
/**
* Sample input #1
*/
// $s = 'hereiamstackerrank';
// $result = hackerrankInString($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 = hackerrankInString($s);
fwrite($fptr, $result . "\n");
}
fclose($stdin);
fclose($fptr);