Exclude Similarities in List of Strings to extract the Difference

前端 未结 2 1364
無奈伤痛
無奈伤痛 2021-01-26 11:14

I have a list of sentences that are the same except for the Title of the book.

How can I loop through the list and exclude the similarities to find the title of the book

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-26 11:52

    This was an interesting problem, so I've played around with it a little and came up with the following (cumbersome) solution:

    Find the first index where any of the sentences have a different char, then do the same in the reversed sentences, and then use Substring to extract only the different parts of the sentences:

    List ExtractDifferences(List sentences)
    {
        var firstDiffIndex = GetFirstDifferenceIndex(sentences);
        var lastDiffIndex = GetFirstDifferenceIndex(sentences.Select(s => new string(s.Reverse().ToArray())).ToList());
        return sentences.Select(s => s.Substring(firstDiffIndex, s.Length - lastDiffIndex - firstDiffIndex)).ToList();
    }
    
    
    int GetFirstDifferenceIndex(IList strings)
    {
        int firstDifferenceIndex = int.MaxValue;
    
        for (int i = 0; i < strings.Count; i++)
        {
            var current = strings[i];
            var prev = strings[i == 0 ? strings.Count - 1 : i - 1];
    
            var firstDiffIndex = current
                .Select((c, j) => new { CurrentChar = c, Index = j })
                .FirstOrDefault(ci => ci.CurrentChar != prev[ci.Index])
                .Index;
    
            if (firstDiffIndex < firstDifferenceIndex)
            {
                firstDifferenceIndex = firstDiffIndex;
            }
        }
        return firstDifferenceIndex;
    }
    

    I guess the GetFirstDifferenceIndex method can be written differently, perhaps better using linq, but I don't have enough time to play with it.

    You can see a live demo on rextester.

提交回复
热议问题