Efficient Algorithm for String Concatenation with Overlap

前端 未结 12 2228
轻奢々
轻奢々 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 21:57

    Why not just do something like this. First get the first character or word (which is going to signify the overlap) in the three columns.

    Then, start to add the first string to a stringbuffer, one character at a time.

    Each time look to see if you reached a part that is overlapped with the second or third string.

    If so then start concatenating the string that also contains what is in the first string.

    When done start, if no overlap, start with the second string and then the third string.

    So in the second example in the question I will keep d and g in two variables.

    Then, as I add the first string abc come from the first string, then I see that d is also in the second string so I shift to adding from the second string def are added from string 2, then I move on and finish with string 3.

    If you are doing this in a database why not just use a stored procedure to do this?

提交回复
热议问题