[Solved] Remove duplicate phrases [closed]


Try this.

static String removeDuplicatePhrase(String s1, String s2) {
    s1 = s1.trim(); s2 = s2.trim();
    List<String> list1 = List.of(s1.split("\\s+"));
    List<String> list2 = List.of(s2.split("\\s+"));
    int size1 = list1.size(), size2 = list2.size();
    int i = Math.min(size1, size2);
    for (; i > 0; --i)
        if (list1.subList(size1 - i, size1).equals(list2.subList(0, i)))
            break;
    return String.join(" ", list1) + " " + String.join(" ", list2.subList(i, size2));
}

static void test(String s1, String s2) {
    System.out.println("s1     = " + s1);
    System.out.println("s2     = " + s2);
    System.out.println("result = " + removeDuplicatePhrase(s1, s2));
    System.out.println();
}

public static void main(String[] args) {
    test("duplicate duplicate duplicate", "xyz");
    test("duplicate phrases", "duplicate phrases abcd");
    test("duplicate phrases", "phrases abcd");
    test("duplicate phrases x", "duplicate phrases abcd");
    test("duplicate phrases", "duplicate phrases");
    test("duplicate ph", "duplicate phrases");
    test("duplicate phrases", "phrases");
    test("duplicate phrases", "rases");
}

output:

s1     = duplicate duplicate duplicate
s2     = xyz
result = duplicate duplicate duplicate xyz

s1     = duplicate phrases
s2     = duplicate phrases abcd
result = duplicate phrases abcd

s1     = duplicate phrases
s2     = phrases abcd
result = duplicate phrases abcd

s1     = duplicate phrases x
s2     = duplicate phrases abcd
result = duplicate phrases x duplicate phrases abcd

s1     = duplicate phrases
s2     = duplicate phrases
result = duplicate phrases 

s1     = duplicate ph
s2     = duplicate phrases
result = duplicate ph duplicate phrases

s1     = duplicate phrases
s2     = phrases
result = duplicate phrases 

s1     = duplicate phrases
s2     = rases
result = duplicate phrases rases

2

solved Remove duplicate phrases [closed]