-5

For example

String str="**Hello bye bye given given world world**"

should return

"**Hello bye given world**".
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • 2
    You don't need regex, just do `str = str.ReplaceDuplicateStringCaseInsensitive();` obviously you will need to implement that function, but I am sure you get the idea from the minimal information I have provided – musefan Oct 23 '17 at 11:58
  • 'world' and 'world**' are different by the way. – achAmháin Oct 23 '17 at 12:09

1 Answers1

0

You could split the string by using space and and compare the string with previous string occurrences by storing the non duplicate values only in a list.

The code will be as follows.

import java.util.ArrayList;
import java.util.List;

public class Test2 {
    private static final String SPACE = " ";

    public static void main(String[] args) {
        System.out.println(replaceDuplicateString("**Hello Bye bye given given world world**"));
    }

    public static String replaceDuplicateString(String input) {
        List<String> addedList = new ArrayList<>();
        StringBuilder output = new StringBuilder();
        for (String str: input.split(SPACE)) {
            if (!addedList.contains(str.toUpperCase())) {
                output.append(str);
                output.append(' ');
                addedList.add(str.toUpperCase());
            }
        }
        return output.toString().trim();
    }
}

This will print

Hello bye given world

How ever if you change the input to

**Hello bye bye given given world world**

the output will be

**Hello bye given world world**

Since, world is not a duplicate of world**.

Sridhar
  • 1,518
  • 14
  • 27