Home Daily Programming PHP Knowledge How to implement full permutation and combination of strings in PHP? (Pictures + Videos)

How to implement full permutation and combination of strings in PHP? (Pictures + Videos)

Oct 11, 2018 pm 01:46 PM

This article mainly introduces to you PHP string full arrangement algorithmHow to implement it specifically.

I believe everyone is familiar with the concept of total permutation, because it is an essential course knowledge point in high school or college mathematics texts. The total arrangement is to randomly select m (m≤n) elements from n different elements and arrange them in a certain order. It is called an arrangement in which m elements are taken from n different elements. When m=n, all arrangements are The situation is called full arrangement.

So in our PHP learning process, we will also encounter problems about the full arrangement of strings.

Below we will introduce the PHP string full arrangement algorithm in detail through specific code examples.

<?php
$str = &#39;abc&#39;;
// 字符串转换为数组
$a = str_split($str);
// 调用perm函数
perm($a, 0,count($a) - 1);
/**
 * 定义perm函数
 * @param $ar // 排列的字符串
 * @param $k // 初始值
 * @param $m // 最大值
 */
function perm(&$ar, $k, $m)
{
    // 初始值是否等于最大值
    if ($k == $m) {
        // 将数组转换为字符串
        echo join(&#39;&#39;, $ar), PHP_EOL;
    } else {
        // 循环调用函数
        for ($i = $k; $i <= $m; $i++) {
            // 调用swap函数
            swap($ar[$k], $ar[$i]);
            // 递归调用自己
            perm($ar, $k + 1, $m);
            // 再次调用swap函数
            swap($ar[$k], $ar[$i]);
        }
    }
}

function swap(&$a, &$b)
{
    $c = $a;
    $a = $b;
    $b = $c;
}
Copy after login

Here we mainly define two methods, perm and swap function, and annotate the meaning of each step of operation in detail in the code, which is helpful for your reference and understanding.

So in the above code, we involve the recursive algorithm. Friends who are unclear about this knowledge point can refer to this article [How to implement recursive sorting in PHP? ].

Finally accessed through the browser, The strings are all arrangedThe results are as follows:

How to implement full permutation and combination of strings in PHP? (Pictures + Videos)

This article is about PHP realizes the full permutation and combination of stringsThe algorithm introduction has a certain reference value. I hope it will be helpful to friends in need!

If you want to know more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!

The above is the detailed content of How to implement full permutation and combination of strings in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)