-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaking Anagrams.php
55 lines (43 loc) · 1.01 KB
/
Making Anagrams.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
<?php
/**
* @author: syed ashraf ullah
* date: 30/04/2020
* problem: https://www.hackerrank.com/challenges/making-anagrams/problem
*/
// Complete the makingAnagrams function below.
function makingAnagrams($s1, $s2) {
// Convert to char array
$s1 = str_split($s1);
$s2 = str_split($s2);
// current total size of two array
$length = sizeof($s1)+sizeof($s2);
$count = 0;
foreach ($s1 as $val1) {
foreach ($s2 as $key => $val2) {
if ($val1 == $val2) {
$count +=2;
unset($s2[$key]);
break;
}
}
}
return ($length - $count);
}
/**
* Sample #1
*/
// $s1 = 'cde';
// $s2 = 'abc';
// $result = makingAnagrams($s1, $s2);
// echo $result;
// return;
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
$s1 = '';
fscanf($stdin, "%[^\n]", $s1);
$s2 = '';
fscanf($stdin, "%[^\n]", $s2);
$result = makingAnagrams($s1, $s2);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);