這個練習的目的是將字符串轉換爲一個新字符串,其中新字符串中的每一個字符都是"()",若是該字符在原始字符串中出現一次,則替換爲"(",若是該字符在原始字符串中出現不止一次,則爲")".
例子:編碼
"din" => "(((" "recede" => "()()()" "Success" => ")())())" "(( @" => "))(("
規範代碼:code
function duplicate_encode($word){ // ... }
規則:字符串
任務:你須要將編碼後的字符串返回!
祝君好運!
我會在晚上將示例代碼貼出來!it
示例代碼io
function duplicate_encode($word){ $word = str_split(strtolower($word)); $str = ""; foreach($word as $key){ (count(array_keys($word,$key))>1) ? $str .= ")" : $str .= "("; } return $str; }