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 8006195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:25:47+00:00 2026-06-04T17:25:47+00:00

I would like to ask all my fellow programmers regarding only efficiency . I

  • 0

I would like to ask all my fellow programmers regarding only efficiency. I am currently solving problems that could be asked in job interviews and I’ve come across with the famous permutations of a string. The code I’ve written below might be the most common thing in programming history, however, I do not know it’s status since I haven’t checked for any solution.

Long story would the short the program I’ve coded below be a suitable solution? Or can it be made even more efficient. Asking because if I came across one day I would like to be sure that I’ve implement one of the best approaches for this problem.

#include <iostream>
using namespace std;

int fac(int num)
{
    int result=1;
    for(int i=1;i<=num;i++)
        result*=i;
    return result;
}

int main(int argc, const char * argv[])
{
    string str="abcd";
    int limit=fac(str.size());
    int mod=str.size();
    for(int i=0;i<limit;i++){
        swap(str[i%mod],str[(i+1)%mod]);
        cout<<str<<endl;
    }
    return 0;
}
  • 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-06-04T17:25:48+00:00Added an answer on June 4, 2026 at 5:25 pm

    I’ve figured out the solution by using a std::map. Don’t think it is that inefficient;

    #include <iostream>
    #include <map>
    #include <vector>
    using namespace std;
    
    int fac(int num)
    {
        int result=1;
        for(int i=1;i<=num;i++)
            result*=i;
        return result;
    }
    int main(int argc, const char * argv[])
    {
        string str="aabb";
        int limit=fac(str.size());
        int mod=str.size();
        std::map<string,bool>T;
        vector<string>permutations;
        for(int i=0;i<limit;i++){
            if(T[str]==0){
                permutations.push_back(str);
                T[str]=1;
            }
            swap(str[i%mod],str[(i+1)%mod]);
        }
        for(int i=0;i<permutations.size();i++)
            cout<<permutations[i]<<endl;
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask if it is possible to copy/move all the objects
I would like to ask a rather, general, high-level introductory kind of question regarding
I would like to ask if there is a possible way to select all
I would like to ask why the foreach does not insert record of all
First of all, I would like to ask you not to refer me to
I would like to ask my fellow developers of their experience with eclipse. I
First of all i would like to ask for excuse if answer is obvious,
First of all, I would like to ask, CAN YOU integrate Bing Maps within
all. I would like to ask if it is possible to have multiple forms
I would like to ask you if you know any concrete library that is

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.