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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:22:13+00:00 2026-06-16T07:22:13+00:00

I know this question has been asked many times, but I’m looking for a

  • 0

I know this question has been asked many times, but I’m looking for a very fast algorithm to generate all permutations of Strings of length 8. I am trying to generate a String of length 8, where each character in the String can be any of the characters 0-9 or a-z (36 total options). Currently, this is the code I have to do it:

for(idx[2] = 0; idx[2] < ch1.length; idx[2]++)
for(idx[3] = 0; idx[3] < ch1.length; idx[3]++)
    for(idx[4] = 0; idx[4] < ch1.length; idx[4]++)
        for(idx[5] = 0; idx[5] < ch1.length; idx[5]++)
            for(idx[6] = 0; idx[6] < ch1.length; idx[6]++)
                for(idx[7] = 0; idx[7] < ch1.length; idx[7]++)
                    for(idx[8] = 0; idx[8] < ch1.length; idx[8]++)
                        for(idx[9] = 0; idx[9] < ch1.length; idx[9]++) 
                            String name = String.format("%c%c%c%c%c%c%c%c%c%c",ch1[idx[0]],ch2[idx[1]],ch3[idx[2]],ch4[idx[3]],ch5[idx[4]],ch6[idx[5]],ch7[idx[6]],ch8[idx[7]],ch9[idx[8]],ch10[idx[9]]);

As you can see, this code is not pretty by any means. Also, this code can generate 280 thousand Strings per second. I’m looking for an algorithm to do it even faster than that.

I’ve tried a recursive approach, but that seems to run slower than this approach does. Suggestions?

  • 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-16T07:22:15+00:00Added an answer on June 16, 2026 at 7:22 am

    Should be faster (generates way above million outputs per second), and at least it’s definitely more pleasant to read:

    final long count = 36L * 36L * 36L * 36L * 36L * 36L * 36L * 36L;
    
    for (long i = 0; i < count; ++i) {
        String name = StringUtils.leftPad(Long.toString(i, 36), 8, '0');
    }
    

    This exploits the fact that your problem:

    generate a String of length 8, where each character in the String can be any of the characters 0-9 or a-z (36 total options)

    Can be reformulated to:

    Print all numbers from 0 until 36^8 in base-36 system.

    Few notes:

    • output is sorted by definition, nice!

    • I’m using StringUtils.leftPad() for simplicity, see also: How can I pad an integers with zeros on the left?

    • what you are looking for is not really a permutation

    • by exploiting the fact that you generate all subsequent numbers you can easily improve this algorithm even further:

      final int MAX = 36;
      final long count = 1L * MAX * MAX * MAX * MAX * MAX * MAX * MAX * MAX * MAX * MAX;
      
      final char[] alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
      final int[] digits = new int[8];
      final char[] output = "00000000".toCharArray();
      
      for (long i = 0; i < count; ++i) {
          final String name = String.valueOf(output);
      
          // "increment"
          for (int d = 7; d >= 0; --d) {
              digits[d] = (digits[d] + 1) % MAX;
              output[d] = alphabet[digits[d]];
              if (digits[d] > 0) {
                  break;
              }
          }
      
      }
      

    Program above, on my computer, generate more than 30 million strings per second. And there’s still much room for improvement.

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

Sidebar

Related Questions

I know this question has been asked many times but I'm looking for a
I know this question has been asked many times, but I couldn't manage to
I know this question has been asked many times. But from what I read,
I know this question has been asked many times, but my problem is a
I know this question has been asked so many times before. But I just
I know this question has been asked many times, but I am having trouble
I know this question has been asked many times, but I never saw a
I know this question has been asked many times but those examples aren't working
I know that this question has been asked many times ( but in different
I know this question has been asked many times before but I can't find

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.