0

I am trying to figure out how to write a C# function that returns true if there is a "match" between two strings. The numbers in the string will always be sorted in asc order.

string1: 1,3
string2: 1,2,3

The function should return true if all the numbers in string1 are found in string2. In the example above the return value should be true.

string1: 1,2,4
string2: 1,2,3

In the example above the return false should be false because not all numbers in string1 are found in string2.

The base case of string1 being an empty string should always return true no matter what string2 contains.

I am thinking of splitting both strings into arrays and trying to do a match and would also be interested in a regex option. Open to any ideas you may have.

I can handle writing the function so really just looking for ideas on the "best" way to accomplish this. And by "best" I mean the option that you think performances the fastest (and yes, I will test on my hardware for performance before moving into production).

The strings at most would have 10 numbers in them, if that helps. We will never see a string with hundreds of numbers.

11
  • 1
    homework.stackexchange.com? Commented Sep 24, 2014 at 13:25
  • So you're asking for the one that performances the fastest (I love that phrase) but you can write your own functions and you're going to test them anyway? What exactly are you expecting from us? Commented Sep 24, 2014 at 13:26
  • "The strings at most would have 10 numbers in them." Why then are you worried about performance? Commented Sep 24, 2014 at 13:27
  • @user2023861 maybe that method will be executed many many times in production Commented Sep 24, 2014 at 13:28
  • I am looking for ideas on ways to accomplish this. I don't need someone to write the function definition, if/then logic, etc. Just looking for ideas on how to best accomplish the task. Commented Sep 24, 2014 at 13:28

1 Answer 1

6

If the format is strict and you cannot have something like 1, "1,2,3", 2 you can use String.Split and LINQ:

bool allInOneInTwo = !string1.Split(',').Except(string2.Split(',')).Any();

Enumerable.Except returns the set difference of two sequences and is very efficient. Because it's executed lazily Enumerable.Any will return on the first match.

Since you've mentioned that it must be efficient because it's executed up to 100k times per day and that you expect that it takes less than a couple of seconds at most: with your sample above it takes only 140 milliseconds to execute it 100k times on my PC. But both arrays are very small. Except would make a greater difference if they were larger.

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

2 Comments

FYI, string.Split() news up its substrings and therefore requires memory allocations. You can definitely do this without string.Split() if your first concern is performance. The code won't be as readable, but it will avoid memory allocations. You just step through each string char-by-char until either you find a number you're not expecting, or the end of the string.
@user2023861: first, OP mentioned that "most would have 10 numbers in them". Also, don't overrate the cost of memory nowadays. You normally have more than enough and by using it you can use performant set based apporoaches like above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.