Generate a 4-character string, including abcdefg. How can we get all the combinations?

Source: Internet
Author: User
How to implement the Program to store all the possibilities in an array. We can repeat aaaa. It is best to implement PHP to store all the possibilities in an array and how to implement the program.
It can be repeated, such as aaaa. It is best to implement it in PHP.

Reply content:

How to implement the Program to store all the possibilities in an array.
It can be repeated, such as aaaa. It is best to implement it in PHP.

Python

import itertoolsprint list(itertools.permutations(["a","b","c","d","e","f","g"],4))

Php

You can try this:

Http://www.honglei.net /? P = 167

Do you want to consider the existence of aaaa? In fact, we can consider encoding them. Suppose abcdefg corresponds to 0123456 respectively, then all the situations are:

0000 (that is, 0 )~ 6666 all numbersLa.

Ps: in hexadecimal notation

Code directly, four for loops, simple, violent, and fast


  

function dfs($pre, $chars, $arr, $lenArr) {    if(!empty($pre) && in_array(strlen($pre), $lenArr)){ $arr[] = $pre; }    if(!empty($chars)) {        foreach ($chars as $char) {            $tempChars = array();            foreach ($chars as $c) {                if ($c !== $char) { $tempChars[] = $c; }            }            $arr = $this->dfs($pre.$char, $tempChars, $arr, $lenArr);        }    }    return $arr;}function get_combine() {    $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g');    $combineArray = array();    $combineArray = $this->dfs('', $chars, $combineArray, array(4));    echo count($combineArray).'
'; var_dump($combineArray);}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.