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

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

不言
Release: 2023-04-04 10:08:01
forward
2504 people have browsed it

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);
Copy after login

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!

Related labels:
php
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template