Common word from multiple strings

I have 4 words like sushma-test-1-10-bug, sushma-test-1-20-count, sushma-test-1-30-color, sushma-test-1-40-fold

I have to extract common word from given 4 Strings.

Output should be: sushma-test-1

you can try this
testt.xaml (8.2 KB)

Hi @jsushma

Please try this approach

(
	From r In {0}
	Let minStr = strArr.OrderBy(Function(x) x.Length).First() 
	_
	Let ra = (	From swlen In Enumerable.Range(1, minStr.Length).Reverse
					From indx In Enumerable.Range(0, minStr.Length-swlen+1)
					Let patternStr = minStr.Substring(indx, swlen)
					Where strArr.All(Function(x) x.Contains(patternstr))
					Select patternStr )
	_
	Select If(ra.Count>0, ra.First(), "")
).First()

Please refer the xaml

CommonWordFromMultipleStrings.xaml (5.4 KB)

Hi @jsushma ,

Interesting problem.
I was able to find a solution, but its nowhere near as neat as @kumar.varun2 .

Iā€™m sharing it here so that you can explore an alternative(using that term very lightly).

//To get a list of common duplicate words
System.Text.RegularExpressions.Regex.
	Split(String.Join("|",lst_words),"\b").
	GroupBy(Function(g) g.ToString).
	Where(Function(w) w.Count()>1 And
		Not w.Key.Equals("|") And
		Not String.IsNullOrWhiteSpace(w.Key)).
	Select(Function(s) s.Key).ToList()
//To get a list of common duplicate words
String.Join("",System.Text.RegularExpressions.Regex.Split(lst_words.First(),"\b").Where(Function(w) lst_duplicateWords.Contains(w)))
//To find the exact string
String.Join("", str_arrangedSemiCommon.ToCharArray.TakeWhile(Function(s,i) s.ToString.equals(lst_words.First()(i).ToString)))

CommonWordsExtraction.xaml (6.7 KB)

Kind Regards
Ashwin A.K

1 Like