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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:37:36+00:00 2026-05-16T20:37:36+00:00

Since I’m not that familiar with java, I don’t know if there’s a library

  • 0

Since I’m not that familiar with java, I don’t know if there’s a library somewhere that can do this thing. If not, does anybody have any ideas how can this be accomplished?

For instance I have a string “foo” and I want to change the letter f with “f” and “a” so that the function returns a list of strings with values “foo” and “aoo”.

How to deal with it when there’s more of the same letters? “ffoo” into “ffoo”, “afoo”, “faoo”, “aaoo”.

A better explanation:
((“a”,(“a”,”b)),(“c”,(“c”,”d”)))
Above is a group of characters that need to be replaced with a character from the other element. “a” is to be replaced with “a” and with “b”. “c” is to be replaced with “c” and “d”.

If I have a string “ac”, the resulting combinations I need are:
“ac”
“bc”
“ad”
“bd”

If the string is “IaJaKc”, the resulting combinations are:
“IaJaKc”
“IbJaKc”
“IaJbKc”
“IbJbKc”
“IaJaKd”
“IbJaKd”
“IaJbKd”
“IbJbKd”

The number of combinations can be calculated like this:
(replacements_of_a^letter_amount_a)*(replacements_of_c^letter_amount_c)
first case: 2^1*2^1 = 4
second case: 2^2*2^1 = 8

If, say, the group is ((“a”,(“a”,”b)),(“c”,(“c”,”d”,”e”))), and the string is “aac”, the number of combinations is:
2^2*3^1 = 12

  • 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-16T20:37:36+00:00Added an answer on May 16, 2026 at 8:37 pm

    Here it is:

    public static void returnVariants(String input){
            List<String> output = new ArrayList<String>();
            StringBuffer word = new StringBuffer(input);
            output.add(input);
    
            String letters = "ac";
            int lettersLength = letters.length();
            int wordLength = word.length();
            String replacement = "";
    
            for (int i = 0; i < lettersLength; i++) {
                for (int j = 0; j < wordLength; j++) {
                    if(word.charAt(j)==letters.charAt(i)){
                        if (word.charAt(j)=='a'){
                            replacement = "ab";
                        }else if (word.charAt(j)=='c'){
                            replacement = "cd";
                        }
                        List<String> tempList = new ArrayList<String>();
                        for (int k = 0; k < replacement.length(); k++) {
                            for (String variant : output){
                                StringBuffer tempBuffer = new StringBuffer(variant);
                                String combination = tempBuffer.replace(j, j+1, replacement.substring(k, k+1)).toString();
                                tempList.add(combination);
                            }
                        }
                        output.addAll(tempList);
                        if (j==0){
                            output.remove(0);
                        }
                    }
                }
            }
            Set<String> uniqueCombinations = new HashSet(output);
            System.out.println(uniqueCombinations);
        }
    

    If input is “ac”, the combinations returned are “ac”, “bc”, “ad”, “bd”. If it can be optimized further, any additional help is welcome and appreciated.

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

Sidebar

Related Questions

Since CS3 doesn't have a web service component, as previous versions had, is there
Since Rails is not multithreaded (yet), it seems like a threaded web framework would
Since the release of Adobe AIR I am wondering why Java Web Start has
Since I started studying object-oriented programming, I frequently read articles/blogs saying functions are better,
Since both a Table Scan and a Clustered Index Scan essentially scan all records
Since Graduating from a very small school in 2006 with a badly shaped &
Since the WMI class Win32_OperatingSystem only includes OSArchitecture in Windows Vista, I quickly wrote
Since debate without meaningful terms is meaningless , I figured I would point at
Since the keyboard is the interface we use to the computer, I've always thought
Since the only operations required for a container to be used in a stack

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.