-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneWinTeam.php
More file actions
66 lines (58 loc) · 1.63 KB
/
OneWinTeam.php
File metadata and controls
66 lines (58 loc) · 1.63 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
66
<?php
require "helpers.php";
/**
* Imagine a tournament where teams are marked with letters from a to z.
* Our task is to find the team with just one win in the entire tournament and return its index (0-indexed).
* If there are multiple teams with just one win, we return the one with the earliest win.
* However, if there isn’t a single team with just one win, we return -1.
*
* For instance, consider the following examples:
* Example 1:
* input $s = “acabsecs”;
* output: 3
*
* Example 2:
* input $s = “finse”;
* output: 0
*
* Example 3:
* input $s = “aabb”;
* output: -1
*
* @param $s
* @return int|mixed
*/
function findTeamWithOneWin($s) {
$wins = [];
$len = strlen($s);
for ($i = 0; $i < $len; $i++) {
$team = $s[$i];
if (isset($wins[$team])) {
if ($wins[$team]['count'] == 1) {
unset($wins[$team]);
} else {
$wins[$team]['count']++;
}
} else {
$wins[$team] = ['count' => 1, 'index' => $i];
}
}
$minIndex = $len;
$minTeam = null;
foreach ($wins as $team => $info) {
if ($info['count'] == 1 && $info['index'] < $minIndex) {
$minIndex = $info['index'];
$minTeam = $team;
}
}
return ($minTeam !== null) ? $minIndex : -1;
}
$teams = 'acabsecs';
$expectedOutput = 3;
checkResult(findTeamWithOneWin($teams) === $expectedOutput, 1);
$teams = 'finse';
$expectedOutput = 0;
checkResult(findTeamWithOneWin($teams) === $expectedOutput, 2);
$teams = 'aabb';
$expectedOutput = -1;
checkResult(findTeamWithOneWin($teams) === $expectedOutput, 3);