-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathduplicate_encode.php
More file actions
46 lines (44 loc) · 1.26 KB
/
duplicate_encode.php
File metadata and controls
46 lines (44 loc) · 1.26 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
<?php
// 6 kyu - Duplicate Encoder
// The goal of this exercise is to convert a string to a new string where each character in the new string is '(' if that character appears only once in the original string, or ')' if that character appears more than once in the original string. Ignore capitalization when determining if a character is a duplicate.
//
// Examples:
//
// "din" => "((("
//
// "recede" => "()()()"
//
// "Success" => ")())())"
//
// "(( @" => "))(("
function duplicate_encode($word){
$arr = str_split(strtolower($word));
foreach($arr as &$val) {
$val = substr_count(strtolower($word), $val) === 1 ? '(' : ')';
}
return implode('',$arr);
}
// Alternative Solutions:
// function duplicate_encode($word){
// $word = str_split(strtolower($word));
// $str = "";
// foreach($word as $key){
// (count(array_keys($word,$key))>1) ? $str .= ")" : $str .= "(";
// }
// return $str;
// }
//
// function duplicate_encode(string $word) : string
// {
// $splittedWord = str_split(strtolower($word));
// $countMap = array_count_values($splittedWord);
//
// $finalString = '';
//
// foreach ($splittedWord as $letter) {
// ($countMap[$letter] === 1) ? $finalString .= '(' : $finalString .= ')';
// }
//
// return $finalString;
// }
?>