Home  >  Article  >  Backend Development  >  How to implement the minimum lexicographic order of splicing in PHP (code)

How to implement the minimum lexicographic order of splicing in PHP (code)

不言
不言forward
2018-10-16 16:25:372417browse

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,&#39;customComp&#39;);
        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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete