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?
 
     
     
     
    