Efficient Algorithm for String Concatenation with Overlap

前端 未结 12 2230
轻奢々
轻奢々 2020-12-02 21:32

We need to combine 3 columns in a database by concatenation. However, the 3 columns may contain overlapping parts and the parts should not be duplicated. For example,

<
12条回答
  •  再見小時候
    2020-12-02 22:02

    I'm trying to make this C# as pleasant to read as possible.

        public static string Concatenate(string s1, string s2)
        {
            if (string.IsNullOrEmpty(s1)) return s2;
            if (string.IsNullOrEmpty(s2)) return s1;
            if (s1.Contains(s2)) return s1;
            if (s2.Contains(s1)) return s2;
    
            char endChar = s1.ToCharArray().Last();
            char startChar = s2.ToCharArray().First();
    
            int s1FirstIndexOfStartChar = s1.IndexOf(startChar);
            int overlapLength = s1.Length - s1FirstIndexOfStartChar;
    
            while (overlapLength >= 0 && s1FirstIndexOfStartChar >=0)
            {
                if (CheckOverlap(s1, s2, overlapLength))
                {
                    return s1 + s2.Substring(overlapLength);
                }
    
                s1FirstIndexOfStartChar = 
                    s1.IndexOf(startChar, s1FirstIndexOfStartChar);
                overlapLength = s1.Length - s1FirstIndexOfStartChar;
    
            }
    
            return s1 + s2;
        }
    
        private static bool CheckOverlap(string s1, string s2, int overlapLength)
        {
            if (overlapLength <= 0)
                return false;
    
            if (s1.Substring(s1.Length - overlapLength) == 
                s2.Substring(0, overlapLength))
                return true;
    
            return false;            
        }
    

    EDIT: I see that this is almost the same as jerryjvl's solution. The only difference is that this will work with the "abcde", "d" case.

提交回复
热议问题