I want to generate unique alphanumeric string of length 28 from two unique alphanumeric strings. Is it mathematically possible to have collision free string from two unique strings?
here is what I did,
ASCII_NUMBER_RANGE_START = 48;
ASCII_ALPHABET_RANGE_START =55;
for (int i = 0; i < firstArray.length; i++) {
int tempASCIIValue = (Character.getNumericValue(firstArray[i]) + Character.getNumericValue(secondArray[i])) % 35;
if (tempASCIIValue <= 9) {
FINAL_ASCII_VALUE = tempASCIIValue + ASCII_NUMBER_RANGE_START;
} else {
FINAL_ASCII_VALUE = tempASCIIValue + ASCII_ALPHABET_RANGE_START;
}
combinedArray[i] = (char) FINAL_ASCII_VALUE;
}
return new String(combinedArray);
}
In above code, i am not sure whether the resultant string is as uniquely strong as its parent strings.
Note: the generated string to have same length as the parent string
Any help is appreciated. Thanks.
Given that collisions are unavoidable. We can look at ideas like hash code generation. In a hashtable, you want to generate a hash code for each object. Ideally you want a Perfect hash function, but thats quite tricky to implement.
You might be able to get away with a hash function see for example Best implementation for hashCode method. A simple one with two integer variables is
int generateHashCode(int a,int b) {
// Start with a non-zero constant. Prime is preferred
int result = 17;
// For each field multiply the previous result by a prime and add
result = 31 * result + a;
result = 31 * result + b;
return result;
}
For your implementation you could change this to work with character. If you are happy to lose one character giving 26 + 26 + 9 = 64 possibilities for each character. This means you can use 6 bits for each character and 168 bits for the whole input, that can be fit into 6 integer. Then just run the generateHashCode()
method on each pair of integers.