Passed
Branch master (a9ed3d)
by Daniel
23:14 queued 31s
created

altlongest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters,
4
5
    each taken only once - coming from s1 or s2.
6
7
Examples:
8
9
a = "xyaabbbccccdefww"
10
b = "xxxxyyyyabklmopq"
11
longest(a, b) -> "abcdefklmopqwxy"
12
13
a = "abcdefghijklmnopqrstuvwxyz"
14
longest(a, a) -> "abcdefghijklmnopqrstuvwxyz"
15
*/
16
17
function longest($stringone, $stringtwo)
18
{
19
    $combinedString = $stringone . $stringtwo;
20
    $stringonerrayString = str_split($combinedString);
21
    $uniqueString = array_unique($stringonerrayString);
0 ignored issues
show
Bug introduced by
It seems like $stringonerrayString can also be of type true; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
    $uniqueString = array_unique(/** @scrutinizer ignore-type */ $stringonerrayString);
Loading history...
22
    sort($uniqueString);
23
    return implode($uniqueString);
24
}
25
26
// Alternate solution
27
28
function altlongest($stringone, $stringtwo)
29
{
30
    $chars = array_unique(str_split($stringone . $stringtwo));
0 ignored issues
show
Bug introduced by
It seems like str_split($stringone . $stringtwo) can also be of type true; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
    $chars = array_unique(/** @scrutinizer ignore-type */ str_split($stringone . $stringtwo));
Loading history...
31
    sort($chars);
32
    return implode('', $chars);
33
}
34