TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Phillip Tan
NA
8
0
How to create different unique combinations from a string?
Mar 11 2008 10:39 PM
Hi there, I've been trying to solve an issue at work or a project.
How do I create multiple unique combinations of a single string? It's supposed to be for a string of 4 digits. e.g. 1234.
Okay, I have managed to solve the part of generating combinations for 4 different digits and 3 same digits (e.g. 1234, 1112).
The problem I have right now is, when I key in 1122 into the textbox and click generate, I will end up getting combinations that are wrong (1112, 2221, 1111, 2222).
The whole idea is to generate combinations that will still retain the same digits from the input.
Can anyone help me with this problem? I've copied my codes below.
private char [ ] chDigits = new char [ 4 ];
private string [ ] permutated4DNumbers;
// Split the 4D number into an array of characters.
chDigits = bet4D.ToCharArray ( 0, bet4D.Length );
permutated4DNumbers = new string [ 24 ];
Random rand = new Random ( );
// A simple variable used for calculating the total number of possible combinations.
int j = 1;
// An index variable for looping through the permutated4DNumbers [] array.
int i = 0;
// This loop will be used to determine the total number of permutations possible.
while (i < 24)
{
// Generate the possible combinations using a random index for each character of the 4D number.
string str4DCombination = chDigits [ rand.Next ( 0, 4 ) ].ToString ( ) + chDigits [ rand.Next ( 0, 4 ) ].ToString ( ) + chDigits [ rand.Next ( 0, 4 ) ].ToString ( ) + chDigits [ rand.Next ( 0, 4 ) ].ToString ( );
bool isInArray = false;
if ( permutated4DNumbers.Length > 0 )
{
foreach ( string betnumber in permutated4DNumbers )
{
// Check if this combination, str4DCombination, generated a few lines above, is already in the array.
if ( betnumber == str4DCombination )
{
isInArray = true;
break;
}
}
}
if ( !isInArray )
{
// if str4DCombination is not in the array and it contains all the numbers from the original 4D number, then add it into the array.
if ( str4DCombination.Contains ( chDigits [ 0 ].ToString ( ) ) && str4DCombination.Contains ( chDigits [ 1 ].ToString ( ) ) && str4DCombination.Contains ( chDigits [ 2 ].ToString ( ) ) && str4DCombination.Contains ( chDigits [ 3 ].ToString ( ) ) )
{
permutated4DNumbers [ i ] = str4DCombination;
j++;
}
}
i++;
}
Reply
Answers (
5
)
Fastest way to render text
How do I refer to key/value pairs from Resource file that i have compiled into DLL?