Home >Backend Development >PHP Tutorial >How to implement the minimum lexicographic order of splicing in PHP (code)
The content of this article is about how to implement the minimum dictionary order (code) of splicing in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
Splicing minimum lexicographic order:
Given an array strs of string type, please find a splicing order such that Concatenate all the strings to form a large string with the smallest lexicographic order among all possibilities and put it back into this large string.
Ideas:
1. Dictionary order, the five numbers 12345 are arranged in different orders. The first one in all the arrangements is 12345, and the last one is 12345. It's 54321.
2. Use the comparison function usort(arr,'costomcomp') to customize the size comparison function, costomcomp(a,b) return a b > b a
3.str_split convert a single string to an array
4 .Character to ascii ord()
<?php function customComp($a,$b){ $a=ord($a); $b=ord($b); $res=$a > $b; var_dump($res); return $res; } //自定义排序 function dictSort($strs) { $strs=str_split($strs); usort($strs,'customComp'); return implode("",$strs); } $arr="cdab"; $result=dictSort($arr); var_dump($result);
The above is the detailed content of How to implement the minimum lexicographic order of splicing in PHP (code). For more information, please follow other related articles on the PHP Chinese website!