I have two strings containing letters and numbers separated by spaces. ex "elza7ma wa2fa fel matab" and "2ana ba7eb el za7ma 2awy 2awy"
What is the fastest way to compare this two string to find out whether or not they have a word in common?
I tried to split one of them using string.split and use string.compare on the whole array of words. but this is very slow since I will be comparing a lot of strings.
A LINQ solution
"elza7ma wa2fa fel matab".Split()
.Intersect("2ana ba7eb el za7ma 2awy 2awy".Split())
.Any();
// as a string extension method
public static class StringExtensions
{
public static bool OneWordMatches(this string theString, string otherString)
{
return theString.Split().Intersect(otherString.Split()).Any();
}
}
// returns true
"elza7ma wa2fa fel matab 2ana".OneWordMatches("2ana ba7eb el za7ma 2awy 2awy");
I think the easiest way is to break up the strings into words and use a set structure like HashSet<string>
to check for duplicates. For example
public bool HasMatchingWord(string left, string right) {
var hashSet = new HashSet<string>(
left.Split(" ", StringSplitOptions.RemoveEmptyEntries));
return right
.Split(" ", StringSplitOptions.RemoveEmptyEntries)
.Any(x => hashSet.Contains(x));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With