Created
January 16, 2020 17:35
-
-
Save khoa-le/4597477100f217ab605702132b8a018f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $A = ["accaccqweara","khanhminh"]; | |
| function buildArraySortedByOccurrence($string) | |
| { | |
| $data = []; | |
| $arrayN = str_split($string); | |
| foreach ($arrayN as $char) { | |
| if (!isset($data[$char])) { | |
| $data[$char] = 1; | |
| } else { | |
| $data[$char]++; | |
| } | |
| } | |
| arsort($data); | |
| return $data; | |
| } | |
| function buildArraySortedByAlphabetInTheSameOccurrence($array) | |
| { | |
| $dataCount = []; | |
| foreach ($array as $alphabet => $count) { | |
| if (!isset($dataCount[$count])) { | |
| $dataCount[$count] = [$alphabet]; | |
| } else { | |
| $merge = array_merge($dataCount[$count], [$alphabet]); | |
| asort($merge); | |
| $dataCount[$count] = $merge; | |
| } | |
| } | |
| return $dataCount; | |
| } | |
| function convertResultToString($dataCount) | |
| { | |
| $result = -404; | |
| if (count($dataCount)) { | |
| $result = ""; | |
| foreach ($dataCount as $count => $listChar) { | |
| foreach ($listChar as $char) { | |
| for ($i = 0; $i < $count; $i++) { | |
| $result .= $char; | |
| } | |
| } | |
| } | |
| } | |
| return $result."\n"; | |
| } | |
| foreach($A as $string){ | |
| $array=buildArraySortedByOccurrence($string); | |
| $arrayDataCount=buildArraySortedByAlphabetInTheSameOccurrence($array); | |
| $result= convertResultToString($arrayDataCount); | |
| echo $result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment