-
Notifications
You must be signed in to change notification settings - Fork 0
/
Two Strings.php
58 lines (43 loc) · 1006 Bytes
/
Two Strings.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: 30/04/2020
* problem: https://www.hackerrank.com/challenges/two-strings/problem
*/
// Complete the twoStrings function below.
function twoStrings($s1, $s2) {
$s1 = str_split($s1);
$s2 = str_split($s2);
$arr1 = array_fill(97,123,0);
$arr2 = array_fill(97,123,0);
foreach ($s1 as $val) {
$arr1[ord($val)]++;
}
foreach ($s2 as $val) {
if ($arr1[ord($val)] > 0) {
return 'YES';
}
}
return 'NO';
}
/**
* Sample #1
*/
// $s1 = 'hello';
// $s2 = 'world';
// $result = twoStrings($s1, $s2);
// 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++) {
$s1 = '';
fscanf($stdin, "%[^\n]", $s1);
$s2 = '';
fscanf($stdin, "%[^\n]", $s2);
$result = twoStrings($s1, $s2);
fwrite($fptr, $result . "\n");
}
fclose($stdin);
fclose($fptr);