I am new to Linq and am trying to convert one of my SQL queries to C# to achieve this. Let us say I have the following set of strings:
ABC-pqr-cv3-xa
LKJ-eqq-cb2-ya
POI-qqq-aaa-1
ABC-pqr-cv3-xb
UIO-qqq-xa
LKJ-eqq-cb2-za
POI-qqq-aaa-2
UIO-qqq-xb
LKJ-eqq-cb2-yb
POI-qqq-aaa-3
I want to group these strings based on whether the entire string matches except the last character. Therefore, following is the output I am expecting:
ABC-pqr-cv3-xa -- 1
ABC-pqr-cv3-xb -- 1
LKJ-eqq-cb2-ya -- 2
LKJ-eqq-cb2-yb -- 2
UIO-qqq-xa -- 3
UIO-qqq-xb -- 3
POI-qqq-aaa-1 -- 4
POI-qqq-aaa-2 -- 4
POI-qqq-aaa-3 -- 4
LKJ-eqq-cb2-za -- 5
Doing this naively would require O(n^2) comparisons. Is there a better way to achieve this? The group numbering itself is not of concern. I am currently trying this and will post an answer if I figure out an efficient way.