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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:11:45+00:00 2026-05-26T01:11:45+00:00

Mathematics/algorithms was never my strong point (!) so requesting help on this one. What

  • 0

Mathematics/algorithms was never my strong point (!) so requesting help on this one.

What is the most efficient implementation for a method with the following signature:

/*
 * pairParts.size() > 0
 * pairParts.size() is always an even number
 */
private Set<StringPairGroup> getAllPossibleStringPairGroups(Set<String> pairParts) {
    Set<StringPairGroup> groups = new HashSet<StringPairGroup>();           
    // logic that adds all possible StringPairGroups            
`   return groups;
}

/*
 * StringPair object: first and second values cannot be null.
 * StringPair object: first != second
 * StringPair object: is equal to another if both the first values and both the second values are equal.
 */
public class StringPair {       
    private final String first;
    private final String second;    
    ...
}

/*
 * StringPairGroup object: is equal to another if their StringPair sets exactly match.
 */
public class StringPairGroup {      
   Set<StringPair> stringPairs  
   ...   
}

As an example an input of {‘A’, ‘B’} would return {[AB],[BA]}.
An input of {‘A’, ‘B’, ‘C’, ‘D’} would return
{[AB],[BA],[AC],[CA],[AD],[DA][…], [AB,CD],[BA,CD],[AB,DC],[BA,DC],[AC,DB],[…]}.

I am really just interested in the logic that creates all the possible StringPairGroups
for any set of input Strings. I could probably come up with some sort of brute force implementation but would rather know how to do something a lot ‘cleverer’.

So any hints as to how I would implement that would be useful.

Edit:

Sorry guys I think I may have missed off something quite important. I am really beggining to confuse myself. This is it:

A StringPairGroup cannot contain a repeated ‘pair part’ across all of its StringPairs. Does that make sense?

  • 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-26T01:11:45+00:00Added an answer on May 26, 2026 at 1:11 am

    I understand the question as follows. There is a collection of N different strings (N is even),
    e.g. A, B, C, D.
    A pair is any concatenation of two different strings from this collection.
    So, AB, BA are pairs, but AA is not a pair. A pair is different from another pair
    if the corresponding concatenated strings are different strings. So, AB is different from BA,
    and AB equals AB (obiously). There are N*N – N different pairs that can be built from
    N different strings.

    A group of order k is a set of k different pairs. So, [AB], [CD] are groups of order 1
    because they both contain 1 pair.
    [AB, CD], [BA, CD] are groups of order 2, because they both contain 2 pairs.
    Two groups are equal if and only if they are equal as two sets. So, two equal
    groups have exactly the same pairs; the order of the pairs does not matter. E.g.,
    [AB, CD] and [BA, CD] are different groups because not all pairs in them are equal.
    [AB, CD] and [CD, AB] are two equal groups.

    All groups of order k can be constructed recursively:

    1. Select any pair P of strings
    2. If k = 1 return the groups built from all these pairs
    3. If k > 1:
      3.1 Remove this pair from the collection C(N) of N strings, leaving a collection C(N-2)
      of the remaining strings.
      3.2 Construct all groups of order k-1 from C(N-2) and combine them with the pair P.

    Here is a Java program (the complete code is on github:gist). An executable program in on ideone.

    public static class Pair {
        public String s1, s2;
        public Pair(String s1, String s2) {
            this.s1 = s1; this.s2 = s2;
        }
        public String toString() {
            return s1 + s2;
        }
    }
    
    public static class Group {
        public List<Pair> pairs = new ArrayList<Pair>();
        public Group(Pair p) {pairs.add(p);}
    }
    
    public static List<Group> getGroups(String[] strings, int order) {
        List<Group> groups = new ArrayList<Group>();
        for (int i = 0; i < strings.length; ++i) {
            for (int j = 0; j < strings.length; ++j) {
                if (i != j) {
                    Pair p = new Pair(strings[i], strings[j]);
                    if (order == 1) {
                        groups.add(new Group(p));
                    }
                    else {
                        String[] strings2 = new String[strings.length - 2];
                        for (int k = 0, k2 = 0; k < strings.length; ++k) {
                            if (k != i && k != j) {
                                strings2[k2++] = strings[k];
                            }
                        }
                        List<Group> groups2 = getGroups(strings2, order - 1);
                        for (int k = 0; k < groups2.size(); ++k) {
                            Group g = new Group(p);
                            groups.add(g);
                            Group g2 = groups2.get(k);
                            g.pairs.addAll(g2.pairs);
                        }
                    }
                }
            }            
        }
        return groups;
    }
    

    There are N/2 possible orders. Contruct the groups for all these orders and append them.

    String strings[] = {"A", "B", "C", "D", "E", "F"};
    List<Group> groups = new ArrayList<Group>();
    for (int order = 1; order <= strings.length/2; ++order) {
        List<Group> groups2 = getGroups(strings, order); 
        groups.addAll(groups2);
    }           
    

    The recursive solution is well understandable but less efficient. If your N is large
    then you would need a faster iterative solution. The iterative solution is less
    illustrative than the recursive one and would not be suitable for the presentation here.
    You can consult e.g. Knuth: The Art of Computer Programming, Vol. 4A: Combinatorial Algorithms.

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

Sidebar

Related Questions

Of course one should never compare floating point values that result from a calculation
How can I write mathematics formula in actionscipt2.0? As following: alt text http://www.freeimagehosting.net/uploads/bc5fee2333.png Thanks
A friend who studies pure mathematics ask me to think about the following problem.
I wrote this post Music and Mathematics, finding the Natural and the Pentatonic scales.
I'm asking this questions out of curiostity, since my quick and dirty implementation seems
What are some good resources for learning about DSP (including the mathematics and algorithms
Primality Check is probably one of those tough problems in mathematics. So, whats is
Discrete mathematics (also finite mathematics) deals with topics such as logic, set theory, information
In our discrete mathematics course in my university, the teacher shows his students the
Does anybody know the mathematics behind an attack on DSA where modulus p has

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.