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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:59:53+00:00 2026-05-30T16:59:53+00:00

I’ve made an anagram method to create all the possible combinations of a word,

  • 0

I’ve made an anagram method to create all the possible combinations of a word, but I’d like to be able to check every letter combination.

For example: eastern would produce other variations such as earnest, but I’d also like it to produce variations such as east and eat and ate.

I already have a working dictionary checking whether the combinations are in the dictionary.

public void printAnagrams(String prefix, String word)
{
    if(word.length() == 1) {
        if (words.contains(prefix+word))
            System.out.println(prefix + word);
    }
    else    
    {
        for(int i = 0; i < word.length(); i++)
        {
            String current = word.substring(i, i + 1);
            String before = word.substring(0, i);
            String after = word.substring(i+1);
            printAnagrams(prefix + current, before + after);
        }
    }
}
  • 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-30T16:59:54+00:00Added an answer on May 30, 2026 at 4:59 pm

    Have a set of chars [can be a char[]] and “guess” which is the next letter, and append it to an intermediate sol StringBuilder, which holds the current substring. Invoke the algorithm recursively to find out next chars of the solution.

    Have an index i to indicate which chars can be used [all chars “right” to i].

    At each iteration – print the resulting string, even if you did not the maximal length.

    Pseudo code:

    findPermutations(chars,i,sol):
       print sol
       if (chars.length == i):
           return
       for each j in range(j,chars.length):
           sol.append(chars[i+1])
           swap(chars,i,j) //we cannot use chars[j] anymore.
           findPermutations(chars,j+1,sol)
           swap(chars,i,j) //clean up environment
           sol.removeLast()
    

    Notes:

    1. This solution assumes chars is a set, so if a certain char appears more then once – you are going to have duplicates, but it will still work [generate all possibilities]
    2. A word about complexity: There are exponential number of possibilities, and you want all of them. So, if you try to invoke this method [and actually any algorithm to do it] with more then 20 chars, you are expected to finish generating all possibilities in ~200 years
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.