Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6213291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:40:13+00:00 2026-05-24T06:40:13+00:00

There is a mapping of characters, like so: $replacements = array( array(‘a’, ‘b’), //

  • 0

There is a mapping of characters, like so:

$replacements = array(
    array('a', 'b'), // a => b
    array('a', 'c'), // a => c
    array('b', 'n'),
    array('c', 'x'),
);

And there is an input string, say “cbaa”. How can I get all combinations, where at least one character is replaced to one of its substitutes? In this example, “a” can be replaced to both “b” and “c”, so strings include:

xbaa
cnaa
xbba
cbca
cbab
cbac
...
xnaa
xnac
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:40:14+00:00Added an answer on May 24, 2026 at 6:40 am

    Here is an altered version of the code of Dmitry Tarasov (all credits to him please) which seems to be working properly.

    class Combine {
        private static $_result = array();
    
        public static function run($str, $replacements){
            self::_run($str, $replacements, 0);
            return array_values(array_unique(self::$_result));
        }
    
        private static function _run($str, $replacements, $start){
            self::$_result[] = $str;
            for($i = $start, $l = strlen($str); $i < $l; $i++){ 
                self::_run($str, $replacements, $i+1);    
                if(isset($replacements[$str[$i]])){
                    foreach($replacements[$str[$i]] as $key => $val){
                        $str[$i] = $val;
                        // call recursion
                        self::_run($str, $replacements, $i+1);
                    }
                }
            }
        }
    }
    
    print_r( Combine::run($str, $replacements) );
    

    The private function was introduced to avoid those heavy array operations to be executed multiple times, while they’re not used anywhere but from the root-call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way of mapping data collected on a stream or array to
For methods where ... there exists a static one-to-one mapping between the input and
I have to convert all the latin characters to their corresponding English alphabets. Can
Are there O/R mapping tools for ASP (not ASP.NET) and are these useful in
There seems to be three common approaches for mapping an application end user to
I was wondering if there was any key mapping in Vim to allow me
Is there a way to separate out the domain objects and mapping files into
So, I'm working on a mapping application. In the app there are these toolbars
There is a table Item like, code,name 01,parent1 02,parent2 0101,child11 0102,child12 0201,child21 0202,child22 Create
I have the following code : System.out.println(new String(–’.getBytes(ISO8859_15_FDIS))); The two characters in the first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.