Home > Web Front-end > JS Tutorial > body text

Generate all permutations and combinations of strings through JavaScript functions

藏色散人
Release: 2021-08-03 14:52:13
Original
3501 people have browsed it

Today I will introduce to you how to generate all permutations and combinations of strings through a JavaScript function. The so-called permutations and combinations are the most basic concepts in combinatorics.

First of all, let me give you a brief introduction to permutations and combinations:

1. Arrangement refers to sorting out a specified number of elements from a given number of elements.

2. Combination refers to taking out only a specified number of elements from a given number of elements, regardless of sorting.

The central problem of permutations and combinations is to study the total number of possible situations in permutations and combinations of specified requirements.

I believe everyone has some understanding of permutations and combinations.

Below we will use javascript code to calculate all permutations and combinations of strings.

The complete code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script>
    //编写一个JavaScript函数来生成字符串的所有组合
 function substrings(str1)
    {
        var array1 = [];
        for (var x = 0, y=1; x < str1.length; x++,y++)
        {
            array1[x]=str1.substring(x, y);
        }
        var combi = [];
        var temp= "";
        var slent = Math.pow(2, array1.length);

        for (var i = 0; i < slent ; i++)
        {
            temp= "";
            for (var j=0;j<array1.length;j++) {
                if ((i & Math.pow(2,j))){
                    temp += array1[j];
                }
            }
            if (temp !== "")
            {
                combi.push(temp);
            }
        }
        console.log(combi.join("\n"));
    }

    substrings("dog");

    </script>
</body>
</html>
Copy after login

Here we arrange and combine a sample string dog, and view the generated results as follows:

Generate all permutations and combinations of strings through JavaScript functions

In the above code, we used several key methods, as follows:

1, pow() method: used to calculate y times of x Power, the syntax is "Math.pow(x,y)".

2, push()Method: You can add one or more elements to the end of the array and return the new length. The syntax is "array.push(item1, item2 , ..., itemX)".

3. join() method: used to put all the elements in the array into a string. The elements are separated by the specified delimiter. The syntax is " arrayObject.join(separator)".

Finally, I would like to recommend the classic course "JavaScript Quick Introduction_Jade Girl Heart Sutra Series" on this platform to everyone. It is free for public welfare ~ everyone is welcome to learn ~

The above is the detailed content of Generate all permutations and combinations of strings through JavaScript functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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