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

  • SEARCH
  • Home
  • 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 612459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:53:46+00:00 2026-05-13T17:53:46+00:00

Here’s a problem thats got me stumped (solution wise): Given a str S ,

  • 0

Here’s a problem thats got me stumped (solution wise):

Given a str S, apply character mappings Cm = {a=(m,o,p),d=(q,u),...} and print out all possible combinations using C or C++.

The string can be any length, and the number of character mappings varies, and there won’t be any mappings that map to another map (thus avoiding circular dependencies).

As an example: string abba with mappings a=(e,o), d=(g,h), b=(i) would print:

abba,ebba,obba,abbe,abbo,ebbe,ebbo,obbe,obbo,aiba,aiia,abia,eiba,eiia,...... 
  • 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-13T17:53:46+00:00Added an answer on May 13, 2026 at 5:53 pm

    Definitely possible, not really difficult… but this will generate lots of strings that’s for sure.

    The first thing to remark is that you know how many strings it’s going to generate beforehand, so it’s easy to do some sanity check 🙂

    The second: it sounds like a recursive solution would be easy (like many traversal problems).

    class CharacterMapper
    {
    public:
      CharacterMapper(): mGenerated(), mMapped()
      {
        for (int i = -128, max = 128; i != max; ++i)
          mMapped[i].push_back(i); // 'a' is mapped to 'a' by default
      }
    
      void addMapped(char origin, char target)
      {
        std::string& m = mMapped[origin];
        if (m.find(target) == std::string::npos) m.push_back(target);
      } // addMapped
    
      void addMapped(char origin, const std::string& target)
      {
        for (size_t i = 0, max = target.size(); i != max; ++i) this->addMapped(origin, target[i]);
      } // addMapped
    
      void execute(const std::string& original)
      {
        mGenerated.clear();
        this->next(original, 0);
        this->sanityCheck(original);
        this->print(original);
      }
    
    private:
      void next(std::string original, size_t index)
      {
        if (index == original.size())
        {
          mGenerated.push_back(original);
        }
        else
        {
          const std::string& m = mMapped[original[index]];
          for (size_t i = 0, max = m.size(); i != max; ++i)
            this->next( original.substr(0, index) + m[i] + original.substr(index+1), index+1 );
        }
      } // next
    
      void sanityCheck(const std::string& original)
      {
        size_t total = 1;
        for (size_t i = 0, max = original.size(); i != max; ++i)
          total *= mMapped[original[i]].size();
    
        if (total != mGenerated.size())
          std::cout << "Failure: should have found " << total << " words, found " << mGenerated.size() << std::endl;
      }
    
      void print(const std::string& original) const
      {
        typedef std::map<char, std::string>::const_iterator map_iterator;
        typedef std::vector<std::string>::const_iterator vector_iterator;
    
        std::cout << "Original: " << original << "\n";
    
        std::cout << "Mapped: {";
        for (map_iterator it = mMapped.begin(), end = mMapped.end(); it != end; ++it)
          if (it->second.size() > 1) std::cout << "'" << it->first << "': '" << it->second.substr(1) << "'";
        std::cout << "}\n";
    
        std::cout << "Generated:\n";
        for (vector_iterator it = mGenerated.begin(), end = mGenerated.end(); it != end; ++it)
          std::cout << "  " << *it << "\n";
      }
    
      std::vector<std::string> mGenerated;
      std::map<char, std::string> mMapped;
    }; // class CharacterMapper
    
    
    int main(int argc, char* argv[])
    {
      CharacterMapper mapper;
      mapper.addMapped('a', "eo");
      mapper.addMapped('d', "gh");
      mapper.addMapped('b', "i");
      mapper.execute("abba");
    }
    

    And here is the output:

    Original: abba
    Mapped: {'a': 'eo''b': 'i''d': 'gh'}
    Generated:
      abba
      abbe
      abbo
      abia
      abie
      abio
      aiba
      aibe
      aibo
      aiia
      aiie
      aiio
      ebba
      ebbe
      ebbo
      ebia
      ebie
      ebio
      eiba
      eibe
      eibo
      eiia
      eiie
      eiio
      obba
      obbe
      obbo
      obia
      obie
      obio
      oiba
      oibe
      oibo
      oiia
      oiie
      oiio
    

    Yeah, rather lengthy, but there’s a lot that does not directly participate to the computation (initialization, checks, printing). The core methods is next which implements the recursion.

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

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could do like: if ($('#name').val().indexOf(',') !== -1) { alert('There… May 14, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer The classic template to write good stories is: "As a… May 14, 2026 at 8:03 pm
  • Editorial Team
    Editorial Team added an answer You could use the display-part of my other answer but,… May 14, 2026 at 8:03 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.