using System.Collections.Generic;
using System.Text.RegularExpressions;
private static readonly Regex WordsRegex = new Regex(@"[\p{L}']+");
private static List<(string word, int presentAt)> MyWords(string text1, string text2) {
HashSet<string> words1 = WordsRegex
.Select(match => match.Value)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
HashSet<string> words2 = WordsRegex
.Select(match => match.Value)
.ToHashSet(StringComparer.OrdinalIgnoreCase);
.Select(word => (word, presentAt: (words1.Contains(word) ? 1 : 0) | (words2.Contains(word) ? 2 : 0)))
public static void Main()
string str1 = "Cat meet's a dog has";
string str2 = "Cat meet's a dog and a bird";
var result = MyWords(str1, str2);
var report = string.Join(Environment.NewLine, result);
Console.WriteLine(report);
string[] options = new string[] {
"present in first string not present in second string",
"not present in first string but present in second string",
"present in first string and present in second string",
var report2 = string.Join(Environment.NewLine, result
.Select(pair => $"{pair.word} - {options[pair.presentAt]}"));