0

This seems like a fairly simple problem, but the solution is eluding me:

If I have the following two strings:

string str1 = "Ping"; string str2 = "Pong"; 

I'm needing an algorithm to create a unique value for the two strings, no matter what order they are in. For example:

UniqueValue(str1, str2) and UniqueValue(str2, str1) should return the exact same value.

EDIT: UniqueValue returns a string. I understand it won't be "truly" unique, but a hash-like value will work.

Thanks, Tyler

4
  • Could you be more specific about the return type of UniqueValue? Commented Jun 22, 2011 at 2:06
  • What would the output of UniqueValue be for these strings? Commented Jun 22, 2011 at 2:06
  • 1
    How unique is unique? Could never be replicated by any other combination? Ping and Pong result in the same unique value until the end of time? What's the resulting type? You have left a lot out. Commented Jun 22, 2011 at 2:09
  • See Justin's answer. It can be truly, truly unique, and you don't have to settle for a hash if you have O(N) storage. Commented Jun 22, 2011 at 2:27

5 Answers 5

7

Sort the strings.

Convert / to // and ; to /; in each of the strings.

Concatenate the strings, putting a ; between each.

Sign up to request clarification or add additional context in comments.

2 Comments

This guarantees that the created value is unique, and that the combinations "ab" "cdef" and "abc" "def" produce different results, even if they contan the separator character (i.e., ;). The downside is that it will be as long as the strings themselves, but that's necessary if you want the hash to be one-to-one.
Excellent! Beat me to the punch.
3

You can Xor the hashcodes.

static string UniqueValue (string str1, string str2) { return (str1.GetHashCode () ^ str2.GetHashCode()).ToString (); } 

Edit: Or, you can generate this string:

if (str2 > str1) return str1+str2; return str2+str1; 

4 Comments

Alternatively, you could concatenate the strings and sort them.
I think xoring is better. Sorting would introduce unneeded overhead.
Your edit produces the same result for "ab" + "cdef" and "abc" + "def". You need a unique separator, and to escape that separator where it may exist in the composing strings.
As I understood the question, the result had to be the same for given input, not unique accross the domain.
0

I would use some sort of hash, but I don't see any algorithm that would produce a 100% guaranteed unique string that could never be duplicated by any other two strings.

Comments

0

I would hash them with SHA1: http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1.aspx

To determine in which orientation they should be in when concatenated, use String.CompareTo: http://msdn.microsoft.com/en-us/library/system.string.compareto.aspx.

Comments

0
public int UniqueValue(params string[] strings) { return String.Join("~", strings.AsEnumerable<string>() .OrderBy<string, string>(s => s) .ToArray<string>()) .GetHashCode(); } 

This, of course, is provided that you don't need complete uniqueness and that a hash code is good enough.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.